Be the first user to complete this post
|
Add to List |
Understanding expressjs middleware with a visual example
function(req, res, next){
// Your business logic goes here
}
- One or more middleware functions comprise the middleware stack.
- Order matters. i.e. Each middleware function is executed in the order in which it was defined in your main application file.
- In a middleware function, invoke the third argument i.e. next() to allow the request to be passed on to the subsequent item in the middleware stack(if there are more middleware functions) or the route handler if this was the last middleware function.
- Middleware functions are most suitable to handle common blanket actions like request logging, authentication etc.
function(req, res, next){
// Basic logging for all the routes
console.log(req.method + ':' + req.originalUrl);
next();
};
Also Read:
- What is an npmignore file and what is it used for
- Accessing the request body and session in nodejs using express
- Installing, Listing and Uninstalling packages using npm
- Setup nginx with multi domain websites running on nodejs