question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Questions: Express next() parameter in a function

See original GitHub issue

When i built auth app

My middlewares:

  • protected (for check token)
  • controllers (to validate body data and interact with services)
  • exceptions (sent error status to client if internal error thrown)

when i use next(id) on protected middleware it throws and error and skip controllers. It goes to exceptions middleware (skip the controllers). In my case i need pass id to the expected contollers but it gives error.

and work excatly as expected, when i change my code to

try{
    const {id} = await verifyToken()
    req.id = id
    next()
}catch(error){
    next(error)
}

My question: if i pass a parameter to next() interface, does it redirect to express internal error handler? even if i dont use try-catch statements

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dougwilsoncommented, Mar 16, 2022

Hello, and sorry you are having an issue. When writing a middleware function, the function is called with three arguments: req, res, and next. The next argument is a function for your middleware to call if it wants to proceed to the next middleware, if any. It takes a since argument of an error. If you provide an error argument to the next function, it will skip forward to your next error handling middleware, if any.

You can find out more on our website at http://expressjs.com/en/guide/writing-middleware.html and http://expressjs.com/en/guide/error-handling.html

I hope this helps!

0reactions
dougwilsoncommented, Mar 16, 2022

Yep!

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - What is the parameter "next" used for in Express?
Basically, the next route to be run will be another one that the URL for the request matches. In this case, if another...
Read more >
What is the use of next() function in Express.js - GeeksforGeeks
callback: It is the callback function that contains the request object, response object, and next() function to call the next middleware ...
Read more >
Complete Guide to Express Middleware - Reflectoring
The next() function is a function in the Express router that, when invoked, executes the next middleware in the middleware stack. If the...
Read more >
Writing middleware for use in Express apps
The next() function is not a part of the Node.js or Express API, but is the third argument that is passed to the...
Read more >
Express: middleware function with custom parameters - tsmx
A standard middleware function always follows a signature with the three arguments (req, res, next) where req is the incoming request, res the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found