From 6112bbd6a46931729911077984a1b2449e597652 Mon Sep 17 00:00:00 2001 From: yelishgiri <118233081+yelishgiri@users.noreply.github.com> Date: Fri, 8 Aug 2025 01:44:37 -0500 Subject: [PATCH] docs(route-handlers): add example for next('route') usage --- en/guide/routing.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/en/guide/routing.md b/en/guide/routing.md index 8ff238884a..5281ecb531 100755 --- a/en/guide/routing.md +++ b/en/guide/routing.md @@ -245,6 +245,26 @@ In Express 4.x, 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: