next()
Passes control to the next matching middleware or route handler.
Syntax
next([err | "route"]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
arg | Error | string | No | Pass an Error to jump to error middleware, or `'route'` to skip remaining handlers of the current route. |
Returns
void — Returns nothing; control flows onward.
Examples
app.use((req, res, next) => {
req.startedAt = Date.now();
next();
});
app.get('/', (req, res) => res.send('ok'));
Output
ok
app.get('/users/:id',
(req, res, next) => {
if (!req.params.id) return next('route');
next();
},
(req, res) => res.json({ id: req.params.id }),
);
Output
{"id":"7"}
Notes
Call `next()` exactly once per middleware - calling it twice or
after sending a response throws. `next(err)` skips to error
handlers; `next('route')` skips to the next route. Forgetting to
call it (and not responding) hangs the request.