🚀 Express.js Intermediate

What is the morgan package and how is it used?

Answer

Morgan is an HTTP request logger middleware for Express. Install: npm install morgan. Use: const morgan = require('morgan'); app.use(morgan('dev'));. Predefined formats: dev (colorized, concise for development), combined (Apache-style with IP, user agent — for production logs), tiny (minimal). Morgan logs each request with method, URL, status code, response time, and content length. For production, pipe logs to a file or logging service: morgan('combined', { stream: fs.createWriteStream('access.log', { flags: 'a' }) }). Morgan is one of the first middleware to register so it logs all requests, including those that are rejected by later middleware.