Continue the middleware chain
A middleware performs logging, authentication, parsing, or other work and then allows the request pipeline to continue.
Express.js · Function
Express supplies next as the third argument to middleware. Calling it passes control onward, while special arguments can skip route or router processing or invoke error handling.
Middleware receives next as its third argument. Calling next() hands control to the following middleware or route; calling next with an error transfers control to error-handling middleware, while next('route') skips the remaining callbacks for the current route.
(req, res, next) => { next(); }| Name | Type | Description |
|---|---|---|
value | any | Optional value controlling continuation. Call with no argument to continue normally; pass an error to enter error handling, or use 'route'/'router' for the documented routing skips. |
const express = require('express');
const app = express();
app.use((req, res, next) => {
console.log(`${req.method} ${req.path}`);
next();
});
app.get('/', (req, res) => {
res.send('ok');
});
app.listen(3000);Logs each request, then passes control to the route handler.
A middleware performs logging, authentication, parsing, or other work and then allows the request pipeline to continue.
A route callback determines that another route should handle the request.
A middleware catches a callback-based failure and delegates it to Express error handling.
next is the third argument supplied to an Express middleware function. Calling next() lets Express continue with the next middleware or route in the request-response cycle.
Follow-up: What happens if middleware calls next(error)? Express skips ordinary handlers and looks for error-handling middleware.
A middleware should call next() when it has not completed the request-response cycle. Otherwise, the request can remain unresolved.
Follow-up: How can middleware finish without calling next()? It can send a response or otherwise complete the request-response cycle.
next('route') skips the remaining middleware callbacks for the current route and proceeds to the next matching route. It is supported only for middleware registered with app.METHOD() or router.METHOD().
Follow-up: How can router middleware leave the router? Use next('router') to pass control outside the router instance.
app.METHOD()router.METHOD()middlewareerror-handling middlewarenext('route')next('router')