Group All Methods For One Path
Chaining With `app.route()`
app.route(path) returns a chainable object — read once, attach multiple HTTP methods.
What you'll learn
- Use app.route to chain
- Recognize when this beats separate calls
When a single path has many HTTP methods, app.route(path) reduces
repetition.
The Long Way
app.get("/users/:id", getUser);
app.patch("/users/:id", updateUser);
app.put("/users/:id", replaceUser);
app.delete("/users/:id", deleteUser); The Chained Way
app.route("/users/:id")
.get(getUser)
.patch(updateUser)
.put(replaceUser)
.delete(deleteUser); Same behavior, one path string. Less noise.
With Middleware
app.route("/users/:id")
.all(requireAuth)
.get(getUser)
.delete(requireAdmin, deleteUser); .all() adds middleware to every method on this path.
Same Thing on a Router
Router#route works the same way:
const r = Router();
r.route("/")
.get(listUsers)
.post(createUser);
r.route("/:id")
.get(getUser)
.patch(updateUser)
.delete(deleteUser); When To Use It
For resource files where the same path has 3+ methods, chaining is cleaner. For one-off routes, separate calls are clearer.
Personal style varies. The most important thing is consistency within your project.
Async Handlers →