Notes for Express Middlewares
Updates at Irregular Intervals
const express = require('express');
const app = express();
// app.get is also a middleware, but it is the last one normally
app.get('/users', (req, res, next) => {
res.send('Users Page');
});
- Middleware is able to access the request and the response parameters
Next function `next()`
- This function is important since it must be called from a middleware for the next middleware to be executed
- It runs in order
next()
will not stop the function, therefore, remember to call return
if you want to stop the function- How to use?
// 1
app.use(yourMiddleware);
//2
app.get('/users', yourMiddleware, (req, res) => {
// ...
});
function yourMiddleware(req, res, next) {
// ....
}
References