-
On this page Using middleware the code look like this: app.use((req, res, next) => {
console.log('Time:', Date.now())
next()
}) but on error handling page it's: app.use((err, req, res, next) => {
// logic
}) which one is correct? Why there are two signatures? Does express.js check the number of arguments? What if I want an error handler and don't use the last parameter because I don't use it? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Yes, express checks the number of arguments. 3 arguments - normal middleware, 4 - error handling middleware.
Declare a function with 4 parameters and don't use the last one. If your linter/typechecker is complaining about it, prefixing it with app.use((err, req, res, _next) => {
// handle the error without using next
} |
Beta Was this translation helpful? Give feedback.
Yes, express checks the number of arguments. 3 arguments - normal middleware, 4 - error handling middleware.
Declare a function with 4 parameters and don't use the last one. If your linter/typechecker is complaining about it, prefixing it with
_
may help.