1)Express app receives a request when someone hits a server for which it will create
request and response.
2)middleware is used to manipulate request.
3)It is middleware because it is a function that run between request and response cycle.
4) middleware stack. middleware that appear first will run first.
5)middleware is like pipeline which end with response object.
//Creating own middleware
//this middleware apply to each and every request
//so, if we put in last then it will not work.
app.use((req,res,next)=> {
console.log('Hello from middleware')
next()
})
//adding request time
app.use((req,res,next)=> {
req.requestTime= new Date().toISOString() //this is method and we need to call that
next()
})
app.use('/user/:id', (req, res, next) => {
console.log('Request Type:', req.method)
next()
})
const express = require('express');
const app = express();
function middlewareFunction(request, response, next){
...
next()
}
app.use(middlewareFunction)