const middleWare = (req, res, next) => {
cosnole.log("I'm in the middle");
next();
};
const handleMain = (req, res) => {
return res.send("this is response and I'm at the end");
};
app.get("/", middleWare, handleMain);
So middleware bascially a clear custom which manage request
app.use(middleWare);
app.get("/", handleMain);
The order of the use() is important. Always put the global middleware top of the other routes.
if you put it under other get requests, It's not gonna work.
- middleware is one function which uses next.
- middleware can be applied in the get() function before the final argument.
app.get("/", middleware, hadleMaine);
- middleware can be used independently, with use() function. (Must be on top of other requests)
app.use(middlewareName);
- middleware makes it possible to control many things. (like who can get the access and who doesn't)