Middlewares
Course: HTTP Deep dive - chapter 5
In Express.js, middleware refers to functions that have access to the request object (req), response object (res), and the next function in the application’s request-response cycle. Middleware functions can perform a variety of tasks, such as
- Modifying the request or response objects.
- Ending the request-response cycle.
- Calling the next middleware function in the stack.

Try running this code and see if the logs comes or not
app.use(function(req, res, next) { console.log("request received"); next();})
app.get("/sum", function(req, res) { const a = parseInt(req.query.a); const b = parseInt(req.query.b);
res.json({ ans: a + b })});Modifying the request
Section titled “Modifying the request”app.use(function(req, res, next) { req.name = "harkirat" next();})
app.get("/sum", function(req, res) { console.log(req.name); const a = parseInt(req.query.a); const b = parseInt(req.query.b);
res.json({ ans: a + b })});Ending the request/response cycle
Section titled “Ending the request/response cycle”app.use(function(req, res, next) { res.json({ message: "You are not allowed" })})
app.get("/sum", function(req, res) { console.log(req.name); const a = parseInt(req.query.a); const b = parseInt(req.query.b);
res.json({ ans: a + b })});Calling the next middleware function in the stack.
Section titled “Calling the next middleware function in the stack.”app.use(function(req, res, next) { console.log("request received"); next();})
app.get("/sum", function(req, res) { const a = parseInt(req.query.a); const b = parseInt(req.query.b);
res.json({ ans: a + b })});