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

Express.js · Function

use

app.use() binds application-level middleware to an Express app, globally or under a path. Middleware can handle a request, modify it, end the response, or pass control onward with next().

How it works

app.use() attaches middleware to an Express application. It can apply middleware globally or under a path, and middleware controls whether processing ends or continues by sending a response or calling next().

Syntax

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

Parameters

NameTypeDescription
pathstring or path patternPath or route where the middleware applies.
middlewarefunction(req, res, next)Middleware function to invoke for matching requests.

Returns

The documentation does not specify a return value for app.use().

Key points

  • Use app.use() to register middleware on an Express application instance.
  • The middleware argument contains the request-processing function.
  • Middleware may run code, modify req or res, finish the response, or delegate to another middleware.

Example: Path-scoped application middleware

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

app.use('/user/:id', (req, res, next) => {
  console.log('Request Type:', req.method);
  next();
});

app.get('/user/:id', (req, res) => {
  res.send(`User ${req.params.id}`);
});

app.listen(3000);

Mounts application-level middleware for requests matching the path, then passes control to the route handler.

When to use it

Cross-cutting application middleware

Apply logging, authentication, parsing, or other shared processing across application requests.

Path-scoped processing

Run middleware only for a URL subtree such as /user/:id.

Middleware chains

Compose several middleware functions into an ordered request pipeline.

Interview questions

QWhat does app.use() do?

app.use() binds application-level middleware to an Express app. With a path argument, the middleware applies to requests matching that path; without one, it can run for every request.

Follow-up: How do you attach middleware to only one HTTP method? Use app.METHOD(), such as app.get() or app.post(), with the method-specific handler.

QHow does middleware continue request processing?

A middleware function receives req, res, and next. It can execute code, modify the request or response, end the cycle, or call next() to transfer control to the following middleware.

Follow-up: What happens if it neither ends the cycle nor calls next()? The request remains unresolved and can hang.

QWhy does middleware order matter?

Middleware is executed in registration order, so app.use() placement determines when it runs relative to other middleware and routes. A path-scoped middleware can inspect route parameters when mounted appropriately.

Follow-up: How is router-level middleware expressed? Use router.use() and router.METHOD() on an Express Router instance.

  • app.METHOD()
  • router.use()
  • router.METHOD()
  • express.Router()

Source: Using middleware. Checked 2026-10-15.