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:
- Configure The 'script' tag In package.json To Run Multiple Commands
- What is an npmignore file and what is it used for
- Send email using nodejs and express in 5 simple steps
- Dynamic module loading with require
- Scaling a basic nodejs application using clusters