Book Image

Mastering Web Application Development with Express

By : Alexandru Vladutu
Book Image

Mastering Web Application Development with Express

By: Alexandru Vladutu

Overview of this book

Table of Contents (18 chapters)
Mastering Web Application Development with Express
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The functionality of middleware


The middleware function takes the following arguments:

  • The request object: This is a wrapper on top of the request parameter found in Node's http.createServer function, with added functionalities

  • The response object: This is another wrapper that extends the response parameter found in Node's http.createServer function

  • A callback: This is usually named next, which might get executed when everything in the current middleware is done so that the following middleware in the stack can be invoked

The following is an example of a middleware that only allows the web application to be accessed by users that are inside the private network, based on their IP address. If a user has access, we will call the next function; otherwise, we will display an error message and send the error status code (403 in this case).

function restrictAccess(req, res, next) {
  var ip = req.ip;

  // check if the ip belongs to the server
  // or to a user in the local network
  // meaning his...