Express provides a simple but effective middleware framework that allows you to provide additional functionality between the point where a request is received and when you actually handle the request and send the response. Middleware allows you to apply authentication, cookies, and sessions and to otherwise manipulate a request before it is passed to a handler.
Express is built on top of the connect
NPM module and provides the underlying middleware support provided by connect
. The following are some of the middleware components that are supported by Express:
static: Allows the Express server to stream static file GET
requests. This middleware is built into express and can be accessed via express.static()
.
express-logger: Implements a formatted request logger to track requests to the server.
basic-auth-connect: Provides support for basic HTTP authentication.
cookie-parser: Allows you to read cookies from a request and set cookies in the response.
cookie-session: Provides cookie-based session support.
express-session: Provides a fairly robust session implementation.
body-parser: Parses JSON data in the body of POST
requests into the req.body
property.
compression: Provides Gzip compress support for large responses to a client.
csurf: Provides cross-site request forgery protection.
For the examples in this book, you will need to install the following versions of Express middleware modules using the npm install
command:
npm install [email protected]
npm install [email protected]
npm install [email protected]
npm install [email protected]
npm install [email protected]
npm install [email protected]
You can also add express
to your package.json
module to ensure that these modules are installed when you deploy your application.
Note
Additional express middleware components are available as NPMs; just query the NPM repository. You can also create your own custom middleware.
You can apply middleware either globally to all routes under a specific path or to a specific route. The following sections describe each of these methods.