CodeBlanks Logo Icon CodeBlanks

app.use

Learn global middleware with app.use.

Global middleware with app.use

app.use(fn) registers a middleware globally for all requests. The registered middleware runs before all routes defined after app.use. Example: app.use((req, res, next) => { req.requestId = 1; next(); });.

This middleware adds a requestId property to every request. Later routes can access it via req.requestId. Global middleware is used for adding request properties (timestamps, request ids), logging and security checks. The key point is app.use order: if a middleware is not registered before route definitions it will not run for those routes.

Order matters

In Express registration order matters. Middlewares and routes run in the order they are registered. If app.use(logger) is called after app.get("/", handler), the logger will not run for that route.

The correct layout: first app.use for global middlewares, then routes. Example: app.use(express.json()); app.post("/data", handler);. Here express.json() is registered first, so req.body is filled in the /data route. If the order is wrong req.body stays undefined. This principle applies to all middlewares: register first, then use.

Practice Add the requestId middleware

Add middleware with app.use((req, res, next) => { req.requestId = 1; next(); }). Define the route app.get("/", (req, res) => res.send("Request: " + req.requestId)). The server closes itself.

Starter code

index.js
const express = require("express");
const app = express();

// Add a global middleware to the app
// Add an identity property to the request, pass to next
// Respond on the root route using that property
// write here

const server = app.listen(3000, () => {
  const http = require("http");
  http.get("http://localhost:3000/", (res) => {
    let body = "";
    res.on("data", (c) => body += c);
    res.on("end", () => { console.log("Reply: " + body); server.close(); });
  });
});

Sample solution

index.js
const express = require("express");
const app = express();

app.use((req, res, next) => {
  req.requestId = 1;
  next();
});
app.get("/", (req, res) => res.send("Request: " + req.requestId));

const server = app.listen(3000, () => {
  const http = require("http");
  http.get("http://localhost:3000/", (res) => {
    let body = "";
    res.on("data", (c) => body += c);
    res.on("end", () => { console.log("Reply: " + body); server.close(); });
  });
});

Expected output

Yanıt: İstek: 1

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Complete the app.use call

Question
app.____((req, res, next) => { req.requestId = 1; next(); });
Answer
app.use((req, res, next) => { req.requestId = 1; next(); });

Explanation: app.use adds global middleware.

2 Match each piece to its role

Term Meaning
app.use Adds global middleware
req.requestId Adds a property to request
next() Continues the chain
req, res, next Middleware parameters

3 When does an app.use middleware run?

index.js
app.use((req, res, next) => { next(); });
  • Only on errors
  • Only on POST requests
  • On every request (for routes registered after it) Correct answer
  • When the server closes

Explanation: It runs on all routes after registration.

All lessons in Intermediate