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

This commit is contained in:
Yelish Giri
2025-12-13 20:57:17 -06:00
committed by GitHub
parent e34d35a9de
commit fabe479e23

View File

@@ -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: