Notes for Express Middlewares

Updates at Irregular Intervals

R4 Cheng
May 11, 2023
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

--

--

R4 Cheng
R4 Cheng

Written by R4 Cheng

「0」が過去で「1」が未来「今」は何処にもない

No responses yet