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 functionalitiesThe response object: This is another wrapper that extends the response parameter found in Node's
http.createServer
functionA 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...