Middleware

The heart of an Express application

Express is a routing and middleware web framework that has minimal functionality of its own

An Express application is essentially a series of middleware function calls.

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. 

Middleware functions can perform the following tasks:

  • Execute any code.
  • Make changes to the request and the response objects.
  • End the request-response cycle.
  • Call the next middleware function in the stack.
// Here is an example of a simple “Hello World” Express 
// application, for which you will define two middleware functions:


var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.listen(3000);

The following figure shows the elements of a middleware function call:

An Express application can use the following types of middleware:

  • Application-level middleware
  • Router-level middleware
  • Error-handling middleware
  • Built-in middleware
  • Third-party middleware

Application-level middleware

// This example shows a middleware function with no mount path. 
// The function is executed every time the app receives a request.

var app = express();

app.use(function (req, res, next) {
  console.log('Time:', Date.now());
  next();
});

continued...

// This example shows a middleware function mounted 
// on the /user/:id path. The function is executed for 
// any type of HTTP request on the /user/:id path.


app.use('/user/:id', function (req, res, next) {
  console.log('Request Type:', req.method);
  next();
});

// This example shows a route and its handler 
// function (middleware system). The function 
// handles GET requests to the /user/:id path.

app.get('/user/:id', function (req, res, next) {
  res.send('USER');
});

Router-level middleware

Router-level middleware works in the same way as application-level middleware, except it is bound to an instance of express.Router().

var router = express.Router();
var express = require('express');
var app = express();
var router = express.Router();

// mount the router on the app
app.use('/', router);

// a middleware function with no mount path. 
// This code is executed for every request to the router
router.use(function (req, res, next) {
  console.log('Time:', Date.now());
  next();
});

app.get('/', function(req, res) {
  res.send('hello world');
});


app.listen(3000, function() {
  console.log('Example app listening on port 3000');
})

Error-handling middleware

// Error-handling middleware always takes 
// four arguments (err, req, res, next))

app.use(function(err, req, res, next) {
  console.error(err.stack);
  res.status(500).send('Something broke!');
});

Define error-handling middleware functions in the same way as other middleware functions.

Built-in middleware

// Error-handling middleware always takes 
// four arguments (err, req, res, next))

app.use(function(err, req, res, next) {
  console.error(err.stack);
  res.status(500).send('Something broke!');
});

Define error-handling middleware functions in the same way as other middleware functions.

List of Built-in Middleware

Third-party middleware

// Install the Node.js module for the required 
// functionality, then load it in your app at 
// the application level or at the router level.

var express = require('express');
var app = express();
var cookieParser = require('cookie-parser');

// load the cookie-parsing middleware
app.use(cookieParser());

Use third-party middleware to add functionality to Express apps

Resources

Middleware

By Joe Karlsson

Middleware

The heart of Express

  • 2,519