Skip to content

Improve clarity of next('route') usage in Route Handlers section #2034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions en/guide/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,26 @@ In Express 4.x, <a href="https://github.com/expressjs/express/issues/2495">the `

You can provide multiple callback functions that behave like [middleware](/{{ page.lang }}/guide/using-middleware.html) to handle a request. The only exception is that these callbacks might invoke `next('route')` to bypass the remaining route callbacks. You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there's no reason to proceed with the current route.

You can use `next('route')` to skip the rest of a route's callbacks and pass control to the next route. For example:

```js
app.get('/user/:id', (req, res, next) => {
if (req.params.id === '0') {
return next('route')
}
res.send(`User ${req.params.id}`)
})

app.get('/user/:id', (req, res) => {
res.send('Special handler for user ID 0')
})
```

In this example:

- `GET /user/5` → handled by first route → sends "User 5"
- `GET /user/0` → first route calls `next('route')`, skipping to the next matching `/user/:id` route

Route handlers can be in the form of a function, an array of functions, or combinations of both, as shown in the following examples.

A single callback function can handle a route. For example:
Expand Down