devnotes.

Router Complete Reference

A quick reference for the Router object's most commonly used methods.

MethodDescription
router.all()Matches all HTTP methods for a given path.
router.METHOD()Routes requests using a specific lowercase HTTP method name.
router.param()Registers a callback that runs for a named route parameter.
router.route()Returns a chainable route instance for defining multiple methods on one path.
router.use()Mounts middleware on the router, optionally scoped to a path.

A typical modular router file

const express = require("express");
const router = express.Router();

router.use((req, res, next) => {
    console.log("Auth check for this router's routes");
    next();
});

router.route("/orders")
    .get((req, res) => res.send("List orders"))
    .post((req, res) => res.send("Create order"));

router.param("orderId", (req, res, next, id) => {
    console.log("Order ID:", id);
    next();
});

router.get("/orders/:orderId", (req, res) => {
    res.send(`Order ${req.params.orderId} details`);
});

module.exports = router;

Reach for router.route() when a path needs several methods, router.param() for shared parameter logic, and router.use() for middleware scoped to that router.

Router Complete Reference | DevNotes