What is middlewares? What is the concept of middlewares in Express.js?
Middleware functions are functions that have access to the request object (req
), the response object (res
), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.
Middleware functions can perform the following tasks:
- Execute any code.
- Make changes to the request and the response objects.
- End the request-response cycle.
- Call the next middleware function in the stack.
Bind application-level middleware to an instance of the app object by using the app.use()
and app.METHOD()
functions, where METHOD
is the HTTP method of the request that the middleware function handles (such as GET
, PUT
, or POST
) in lowercase.
var app = express()
app.use(function (req, res, next) {
console.log('Time:', Date.now())
next()
})
Comments
Post a Comment