Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (19.9k points)

I'm trying to write an express middleware that handles errors. However, I'm not able to define a function with err as an argument. In the express documentation I read the following:

Define error-handling middleware functions in the same way as other middleware functions, except error-handling functions have four arguments instead of three: (err, req, res, next)

But when I define the following function, it seems like it's not invoked:

myFunction = (config) => {

  return myFunction = (err, req, res, next) {

    console.log(err)

  }

}

module.exports = myFunction

The function above isn't invoked when I specify app.use(myFunction(config)) from my express server, but if I remove the err argument it is. Does anyone know why that is the case?

1 Answer

0 votes
by (25.1k points)

You can get it to work. What you need to do is to specify 

app.use(myFunction(config)) 

below your routes in my server.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...