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
- Express Router API
Router methods, route parameters, and mounting paths.
- Express.js guide
Routing, middleware order, and request/response lifecycle.
- Thinkful — Organizing Express (pastes example)
Router/controller structure demonstration project.