Express.js app.use() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Application

Definition

In Express.js, app.use([path], callback) mounts middleware (or a Router) into the app. The function runs when the request path matches — by default for every request if you omit path.

Official rule: with a string path, app.use matches that path and its subpaths (/apple matches /apple/images). app.get('/apple') matches only the whole path /apple. Always call next() unless you end the response.

Syntax

Call app.use in your Express.js app. Start with this line.

Express
app.use(callback)
app.use(path, callback)
app.use(path, ...callbacks)
app.use(path, router)
CallWhat it does
app.use(callback)Primary signature.
app.use(path, callback)Related call form.
app.use(path, ...callbacks)Related call form.
app.use(path, router)Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

app.use(callback)
app.use(path, callback)
app.use(path, ...callbacks)
app.use(path, router)

Parameters

ParameterDescription
pathRequired. Mount path. Strings match the path and subpaths. Omit to run for all requests.
callbackRequired. Middleware (req, res, next), several of them, an array, or a Router / sub-app.

Return Value

app.use() returns the same application object (app), which is usually ignored. Path defaults to '/' (every request) when omitted.

Express
app.use(express.json());
// => app

app.use('/api', apiRouter).use(errorHandler);

Express.js Examples

Example 1 — Global Middleware

Official-style logger: no path → runs for every request.

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

app.use((req, res, next) => {
  console.log('Time:', Date.now());
  next();
});

app.get('/', (req, res) => {
  res.send('Hello');
});

app.listen(3000);

How It Works

  1. Mount globally. No path means every request hits this function.
  2. Log, then continue. next() passes control to the next layer.
  3. Route responds. app.get('/') sends the body.
  4. Without next(). The GET handler would never run.

Example 2 — Path Prefix

String paths match the prefix and subpaths — unlike app.get.

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

app.use('/api', (req, res, next) => {
  console.log('API hit:', req.method, req.url);
  next();
});

app.get('/api/users', (req, res) => {
  res.json([{ id: 1 }]);
});

app.get('/', (req, res) => {
  res.send('home');
});

app.listen(3000);

How It Works

  1. Prefix match. /api covers /api, /api/users, …
  2. Strip mount. Inside the mount, req.url is relative (/users).
  3. Continue. next() reaches app.get('/api/users').
  4. Home skips it. GET / does not enter the /api middleware.

Example 3 — Mount a Router

Typical app shape: built-in JSON parser + feature router.

Express
const express = require('express');
const app = express();
const users = express.Router();

app.use(express.json());

users.get('/', (req, res) => {
  res.json([{ name: 'Ada' }]);
});

users.post('/', (req, res) => {
  res.status(201).json({ name: req.body.name });
});

app.use('/users', users);

app.listen(3000);

How It Works

  1. Parse JSON. app.use(express.json()) fills req.body for POSTs.
  2. Define routes. Paths on the router are relative (/).
  3. Mount. app.use('/users', users) → full URLs /users, …
  4. Same API. Routers are middleware — that is why use accepts them.

Runtime Support

app.use() is an Express.js application method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only send requests into your middleware stack.

✓ Server · Express 4 & 5

app.use()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
app.use() Express on Node.js

Bottom line: Register middleware with app.use, remember next(), then start the server.

Frequently Asked Questions

It mounts middleware (or a Router) into the request pipeline. With a path, it runs when the request path starts with that prefix; without a path, it runs for every request.
app.get matches one verb and the whole path. app.use matches every HTTP method and, for string paths, the path plus subpaths (prefix match).
If it does not end the response, it must call next() so the next middleware or route can run. Otherwise the request hangs.
Yes. app.use('/api', router) is the standard way to attach modular routes under a prefix.
Use four arguments (err, req, res, next). Express only treats it as error middleware when all four are declared — official Caution.

Next: express.json()

Parse JSON request bodies with built-in middleware.

express.json() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful