mirror of
https://github.com/expressjs/expressjs.com.git
synced 2026-02-21 19:41:33 +00:00
docs: Improve clarity of next('route') usage in Route Handlers section (#2034)
This commit is contained in:
@@ -246,6 +246,24 @@ 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.
|
||||
|
||||
```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:
|
||||
|
||||
Reference in New Issue
Block a user