Q6: What are the 5 types of middleware in Express.js?

Difficulty:EasyTopic:Middleware

Express categorizes middleware based on mounting scope and signature:

  1. Application-level: Bound to app.use() or app.METHOD().
  2. Router-level: Bound to router.use() or router.METHOD() instances.
  3. Built-in: Included natively (express.json(), express.urlencoded(), express.static()).
  4. Third-party: Installed via NPM (e.g., cors, helmet, morgan).
  5. Error-handling: Takes 4 parameters (err, req, res, next).
Key Takeaways
  • Application-level middleware applies globally across the app instance.

  • Error-handling middleware is uniquely identified by accepting exactly 4 arguments.