Route chaining

Chaining multiple HTTP verb handlers onto a single Route instance.

Since Express 4 Spec ↗

Syntax

app.route(path).get(fn).post(fn).put(fn)

Returns

Route — The same Route, enabling fluent chaining.

Examples

app.route('/users/:id')
  .all((req, res, next) => {
    req.user = loadUser(req.params.id);
    next();
  })
  .get((req, res) => res.json(req.user))
  .delete((req, res) => {
    deleteUser(req.params.id);
    res.sendStatus(204);
  });
Output
$ curl localhost:3000/users/1
{"id":"1"}
const r = express.Router();
r.route('/')
  .get((req, res) => res.send('list'))
  .post((req, res) => res.status(201).send('created'));
app.use('/items', r);
Output
$ curl -X POST localhost:3000/items
created

Notes

A leading `.all()` is handy for shared setup (auth, resource loading) across every verb on the path. Chaining keeps related handlers together and prevents accidental duplicate route definitions for the same path.

See also