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.
Previous
What is async/await error handling in Express.js?
Next
How do you structure a large Express.js application?
More Express.js Questions
View all →- Intermediate How do you implement JWT authentication in Express.js?
- Intermediate What is Express middleware chaining and how does it work?
- Intermediate What is the helmet package and why should you use it?
- Intermediate How do you implement rate limiting in Express.js?
- Intermediate What is input validation and how do you do it in Express?