Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Express.js · Function

next

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.

How it works

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.

Syntax

(req, res, next) => { next(); }

Parameters

NameTypeDescription
valueanyOptional 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.

Key points

  • next() advances execution to the middleware that follows the current one.
  • next is an injected middleware argument, not a separately imported Express API.
  • next('route') bypasses the remaining callbacks in the current route stack.
  • next('router') exits the current router and resumes processing outside it.

Common mistakes

  • A middleware that neither sends a response nor calls next() leaves the request hanging.
  • Passing an argument other than 'route' makes Express treat the request as being in an error state.
  • Use next('route') only in route middleware registered through an HTTP-method route or router method.

Example: Pass control to the next middleware

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.

When to use it

Continue the middleware chain

A middleware performs logging, authentication, parsing, or other work and then allows the request pipeline to continue.

Skip to the next route

A route callback determines that another route should handle the request.

Forward an error

A middleware catches a callback-based failure and delegates it to Express error handling.

Interview questions

QWhat is next in Express middleware?

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.

QWhen must middleware call next()?

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.

QHow does next('route') differ from next()?

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()
  • middleware
  • error-handling middleware
  • next('route')
  • next('router')

Source: Writing middleware for use in Express apps. Checked 2026-10-15.