How it works...

Express as a web server framework relies on a collection of Node.js modules that serve as middleware layers for our web application's backend. Middleware is simply a pattern for defining an asynchronous operation for a given request type. We will explore how to use and create our own middleware in the Creating Express middleware for routes section of this chapter.

For now, suffice to say that middleware are simply layers of operations on requests within your application, and Express provides a logical way to set the context and order of those layers in concert with the rest of your application. This compositional web server control is what makes Express such a powerful tool, despite its small size and simplicity.

By default, Express only has a single configuration file called /app.js. At only 53 lines long, this file defines everything Express needs to know to start a web server and provide the basic middleware needed to serve content:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var sassMiddleware = require('node-sass-middleware');

var index = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(sassMiddleware({
src: path.join(__dirname, 'public'),
dest: path.join(__dirname, 'public'),
indentedSyntax: true, // true = .sass and false = .scss
sourceMap: true
}));
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;

This is a stark contrast when compared to the complicated relationships of components, modules, services, and assorted class constructs of our Angular application. This configuration file is, remarkably, a vanilla Node.js JavaScript file, with no classes, typing, or unnecessary abstractions. It's this minimalistic aspect that makes Express very popular with both beginner and expert developers alike.

You may note that there are two files called /routes/index.js and /routes/users.js also included in this configuration. These are default routes that are configured to handle the /index and /users routes of our web server. We will not use either of these routes for our web server configuration and will instead explore how to create our own routes in the next recipe: Working with routes in Express.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.188.120