CHAPTER 2

image

Working with Middleware

Middleware is an amazingly useful pattern that allows developers to reuse code within their applications and even share it with others in the form of NPM modules. The essential definition of middleware is a function with three arguments: request (or req), response (res), and next. If you’re writing your own middleware, you can use arbitrary names for arguments, but it’s better to stick to the common naming convention. Here’s an example of how to define your own middleware:

var myMiddleware = function (req, res, next) {  // Do something with req and/or res  next();};

When writing your own middleware, don’t forget ...

Get Express.js Deep API Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.