Well-structured backends separate concerns: routers define URLs, controllers translate HTTP to application calls, services own data access, and middleware handles cross-cutting behavior (validation, 404, errors). Study these patterns to keep files small, testable, and aligned with REST conventions.

How to mount routers with URL prefixes — Intermediate

Use app.use('/prefix', router) to group related endpoints under a base path.
Learn: Mounting groups related endpoints under a prefix (`/users/:userId/pastes`) keeps URLs readable and mirrors resource ownership. Order matters: register specific routes before catch-all middleware like notFound.

Supplementary examples

Nested resource mount

const usersRouter = require("./users/users.router");
const pastesRouter = require("./pastes/pastes.router");

app.use("/users", usersRouter);
app.use("/users/:userId/pastes", pastesRouter);

Course example

const usersRouter = require("./users/users.router");
const pastesRouter = require("./pastes/pastes.router");
app.use("/users", usersRouter);
app.use("/pastes", pastesRouter);

Additional references & examples

← All Building routes · Server Architecture / Routes