Q6: What are the 5 types of middleware in Express.js?
Express categorizes middleware based on mounting scope and signature:
- Application-level: Bound to
app.use()orapp.METHOD(). - Router-level: Bound to
router.use()orrouter.METHOD()instances. - Built-in: Included natively (
express.json(),express.urlencoded(),express.static()). - Third-party: Installed via NPM (e.g.,
cors,helmet,morgan). - 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.