Skip to content

Commit fabe479

Browse files
authored
docs: Improve clarity of next('route') usage in Route Handlers section (#2034)
1 parent e34d35a commit fabe479

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

en/guide/routing.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,24 @@ In Express 4.x, <a href="https://github.com/expressjs/express/issues/2495">the `
246246

247247
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.
248248

249+
```js
250+
app.get('/user/:id', (req, res, next) => {
251+
if (req.params.id === '0') {
252+
return next('route')
253+
}
254+
res.send(`User ${req.params.id}`)
255+
})
256+
257+
app.get('/user/:id', (req, res) => {
258+
res.send('Special handler for user ID 0')
259+
})
260+
```
261+
262+
In this example:
263+
264+
- `GET /user/5` → handled by first route → sends "User 5"
265+
- `GET /user/0` → first route calls `next('route')`, skipping to the next matching `/user/:id` route
266+
249267
Route handlers can be in the form of a function, an array of functions, or combinations of both, as shown in the following examples.
250268

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

0 commit comments

Comments
 (0)