Understanding Middleware

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:

Image static: Allows the Express server to stream static file GET requests. This middleware is built into express and can be accessed via express.static().

Image express-logger: Implements a formatted request logger to track requests to the server.

Image basic-auth-connect: Provides support for basic HTTP authentication.

Image cookie-parser: Allows you to read cookies from a request and set cookies in the response.

Image cookie-session: Provides cookie-based session support.

Image express-session: Provides a fairly robust session implementation.

Image body-parser: Parses JSON data in the body of POST requests into the req.body property.

Image compression: Provides Gzip compress support for large responses to a client.

Image 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.

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

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