Q26: What is route chaining and how do you use `app.route()`?

Difficulty:EasyTopic:Routing

app.route() allows defining chained handlers for a single route path. It reduces code duplication when multiple HTTP methods target the same URI path.

Chaining HTTP Verbs with app.route()
app.route('/api/v1/products')
  .get((req, res) => {
    res.json({ action: 'Get all products' });
  })
  .post((req, res) => {
    res.json({ action: 'Create a new product' });
  })
  .put((req, res) => {
    res.json({ action: 'Update products' });
  });

Chaining routes avoids repeating URL paths across different HTTP verb handlers.

Key Takeaways
  • DRY (Don't Repeat Yourself) route definition pattern.

  • Improves code organization when handling CRUD routes on single endpoints.