Express Routing
Node.js · Reference cheat sheet
Express Routing
Node.js · Reference cheat sheet
📋 Overview
Express matches HTTP method + path patterns to handlers. Use Router to split large apps into mountable modules.
🔧 Core concepts
| Pattern | Matches |
|---|---|
/users | Exact path |
/users/:id | Param req.params.id |
/files/* | Wildcard (version-dependent syntax) |
/ + Router mount | Prefixed paths |
| API | Role |
|---|---|
app.METHOD(path, ...handlers) | Method-specific |
app.all | Any method |
router.route(path) | Chain verbs for one path |
req.params / req.query | Path params / query string |
💡 Examples
Params and query:
app.get("/users/:id", (req, res) => {
res.json({ id: req.params.id, q: req.query.q });
});
// GET /users/42?q=activeRouter module:
import { Router } from "express";
const users = Router();
users.get("/", (req, res) => res.json([]));
users.get("/:id", (req, res) => res.json({ id: req.params.id }));
app.use("/users", users);Route chaining:
router
.route("/items/:id")
.get((req, res) => res.send("get"))
.put((req, res) => res.send("put"))
.delete((req, res) => res.send("delete"));⚠️ Pitfalls
- More specific routes must be registered before generic
/:idcatchers. req.paramsvalues are strings — coerce IDs explicitly.- Trailing-slash behavior depends on settings; be consistent in clients and routes.