Skip to content

What is the function signature of express.js middlewere? #5065

Answered by krzysdz
jcubic asked this question in Q&A
Discussion options

You must be logged in to vote

Does express.js check the number of arguments?

Yes, express checks the number of arguments. 3 arguments - normal middleware, 4 - error handling middleware.

What if I want an error handler and don't use the last parameter because I don't use it?

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.

app.use((err, req, res, _next) => {
   // handle the error without using next
}

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@markcellus
Comment options

@jcubic
Comment options

Answer selected by jcubic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #5065 on December 15, 2022 15:18.