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

Express.js · Module

app.use

app.use() mounts application-level middleware, optionally at a path. Express runs matching middleware in registration order, and each middleware either completes the response or passes control onward with next().

How it works

app.use() adds application-level middleware to the request pipeline. With an optional path, Express runs it when the request path matches that mount point; middleware can inspect or modify the request and response, finish the response, or call next() to continue.

Syntax

app.use([path,] callback [, callback...])

Parameters

NameTypeDescription
pathstring or path patternOptional mount path at which the middleware is applied; defaults to / when omitted.
callbackfunctionOne or more middleware callback functions to execute for matching requests.

Returns

The page documents app.use() as a middleware-registration method but does not specify a return value.

Key points

  • Omitting the path mounts middleware at /, making it eligible for every request.
  • The middleware callback is the function Express invokes during request processing.
  • A common use is mounting static-file middleware behind a virtual /static prefix.

Common mistakes

  • Register middleware in the required order because Express processes the stack sequentially.
  • A mount path also matches descendants: /admin applies to /admin and paths such as /admin/reports.

Example: Mount middleware at a path

const express = require('express');
const app = express();

app.use('/admin', (req, res, next) => {
  console.log(req.method, req.path);
  next();
});

app.get('/admin', (req, res) => res.send('Admin'));
app.listen(3000);

Logs requests under /admin, then passes control to the matching route.

When to use it

Run logging or request preprocessing

Cross-cutting request processing

Apply middleware only under a mount path

Scoped processing

Serve files through mounted static middleware

Static assets

Chain multiple middleware callbacks

Request pipeline composition

Interview questions

QWhat does app.use() do?

app.use() registers application-level middleware. It can be mounted globally or at a path, and the middleware receives req, res, and next.

Follow-up: How would you restrict handling to one HTTP method? Use app.METHOD(), such as app.get(), because app.use() is not method-specific.

QHow does the optional path affect matching?

The first argument may be a mount path; when omitted, the path defaults to /, so the middleware runs for every request. A path such as /user/:id limits execution to matching requests.

Follow-up: What happens for /user/42/profile? It can still match because the request path follows the mounted path with /.

QWhen must middleware call next()?

Middleware can end the request-response cycle or call next() to transfer control to the next middleware. If it does neither, the request remains pending.

Follow-up: Why can registration order change behavior? Earlier middleware executes first and may modify, terminate, or prevent later processing.

  • app.METHOD()
  • app.all()
  • app.route()
  • express.static()
  • router.use()

Source: Application Object - Express.js API Reference. Checked 2026-10-15.