Group resource handlers
Several methods such as GET and POST operate on the same resource path.
Express.js · Module
Creates a chainable Route for one path, allowing handlers for multiple HTTP verbs to be declared together. It keeps the path in one place, reducing duplication and typos.
app.route(path) creates one route object for the supplied path. Chain methods such as .get(), .post(), and .all() on the returned object to attach handlers, keeping the path in one place.
app.route(path)| Name | Type | Description |
|---|---|---|
path | String | The path of the route to create. |
Route
app.route(path) when several HTTP methods belong to the same path.const express = require('express');
const app = express();
app.route('/books')
.get((req, res) => res.json([{ title: 'Dune' }]))
.post((req, res) => res.status(201).send('Created'));
app.listen(3000);Creates one chainable route definition and attaches handlers for multiple HTTP verbs.
Several methods such as GET and POST operate on the same resource path.
A route needs shared middleware before method-specific handlers.
A codebase has repeated path strings across verb declarations.
app.route() return, and how is it used?app.route(path) creates a single Route for the path and returns it. HTTP verb handlers can then be chained on that Route, such as .get() and .post().
Follow-up: Why use it instead of separate app.get() and app.post() calls? It keeps the path in one location, reducing duplicated route names and typo risk.
app.route() accept?Its path argument is a string identifying the route to create. The returned Route supports HTTP verbs with optional middleware.
Follow-up: How would you apply logic to every verb on that path? Chain .all() before the verb-specific handlers.
app.route() useful?It supports chainable handlers for one route path, allowing multiple HTTP methods to be declared together. This makes route definitions more modular and avoids repeating the path.
Follow-up: What alternative provides mountable modular route handlers? Use express.Router() for router-level organization.
app.METHODapp.paramexpress.RouterRoute