What are middlewares in express
Middleware is a simple javascript function with three parameters request, response and next
Apart from handling http request it can perform the following tasks:
- Manipulate request and response objects
- Perform isolated tasks
- Terminate request flow by sending response
- Pass the control to another middleware
We load middleware in express by use() of the express app.
var express = require('express'); var app = express(); app.use(function (req, res, next){ // Your code // to pass the control invoke next() at the end next(); });
Here,
– req is the request object.
– res is the response object.
Click Here to find github example