router.METHOD() defines a route on a Router instance for one specific HTTP method — get,
post, put, delete, and so on.
Syntax
router.METHOD(path, callback [, callback ...])
Example
const express = require("express");
const app = express();
const router = express.Router();
const PORT = 5000;
router.get("/items", (req, res) => {
res.send("List of items");
});
router.post("/items", (req, res) => {
res.send("Item added");
});
router.delete("/items/:id", (req, res) => {
res.send(`Item ${req.params.id} removed`);
});
app.use(router);
app.listen(PORT, () => console.log(`Listening on ${PORT}`));
Use cases
- Defining routes scoped to a specific HTTP method.
- Organizing routes with
express.Router()instead of directly onapp. - Building modular, mountable route files for RESTful APIs.