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 create an Express router — Basic

Instantiate Router(), wire handlers, and export the router for mounting on the app.
Learn: An Express Router is a mini-app: it has its own middleware stack and routes, then mounts on the main app. `mergeParams: true` lets nested routers see parent params (e.g. `:userId` from a parent mount). Always export the router for `app.use`.

Supplementary examples

Router module skeleton

const router = require("express").Router({ mergeParams: true });

// routes here

module.exports = router;

Mount in app.js

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

Course example

const router = require("express").Router({ mergeParams: true });

Additional references & examples

← All Building routes · Server Architecture / Routes