CodeBlanks Logo Icon CodeBlanks

Middleware

Add logging and error middleware.

Middleware

The fourth lesson adds middleware: functions that sit between the request and the response. You define one with app.use((req, res, next) => { ... }). It runs on every request; calling next() passes control to the next middleware or route.

A common use is logging: with console.log(req.method, req.url) you print the method and path of every request. For example, when a GET /books request arrives, GET /books is logged. Middleware must be defined BEFORE routes; otherwise the request reaches the route and the middleware is skipped. If next() is not called, the request hangs. So every middleware must call next() or send a response.

Logging middleware

A logging middleware records every request. You define it with app.use((req, res, next) => { console.log(req.method, req.url); next(); }). It runs on all requests.

First console.log prints the method and path, then next() continues the flow. If you forget next(), the request never reaches any route and the client gets no response. Middleware order matters: a middleware added with app.use only affects routes defined AFTER it. So you must place the logging middleware before the routes. This way each request logs first, then runs the route; both monitoring and debugging become easier.

Practice Add a logging middleware

Define a logging middleware. With app.use((req, res, next) => { ... }) print console.log(req.method, req.url) and call next(). The middleware must come BEFORE the routes. The self-request is ready.

Starter code

index.js
const books = [];
books.push({ id: 1, title: "Node.js" });

const express = require("express");
const app = express();
app.use(express.json());

// Logging middleware — print method and url, pass to next step
// write here

app.get("/books", (req, res) => {
  res.json(books);
});

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

Sample solution

index.js
app.use((req, res, next) => {
  console.log(req.method, req.url);
  next();
});

Expected output

GET /books
[{"id":1,"title":"Node.js"}]

Questions and answers

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

1 What is called to pass control to the next middleware?

index.js
next()
  • res.send()
  • return
  • app.use()
  • next() Correct answer

Explanation: next() passes control to the next middleware or route.

2 Call next

Question
console.log(req.method, req.url); ____();
Answer
console.log(req.method, req.url); next();

Explanation: next() passes the flow to the next step.

3 Match each piece to its role

Term Meaning
app.use Adds middleware
req.method HTTP method
req.url Request path
next() Passes to next step

All lessons in Intermediate