devnotes.

router.route() Function

router.route() returns a chainable route instance for a path, letting you attach several HTTP method handlers to the same path without repeating it.

Syntax

router.route(path)

Example

const express = require("express");
const app = express();
const router = express.Router();
const PORT = 5000;

router.route("/items")
    .get((req, res) => {
        res.send("List of items");
    })
    .post((req, res) => {
        res.send("Item created");
    })
    .put((req, res) => {
        res.send("Item replaced");
    });

app.use(router);

app.listen(PORT, () => console.log(`Listening on ${PORT}`));

Use cases

  • Keeping GET/POST/PUT/DELETE handlers for the same path together.
  • Avoiding repeated path strings across method calls.
  • Building clean, RESTful resource routes.
router.route() Function | DevNotes