0 votes
1 view
in Web Technology by (11.2k 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 (22.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.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...