Morgan: One-Line Request Logging for Express

Morgan is a plug-and-play stenographer for your Express app, automatically logging every incoming HTTP request. Use its predefined formats for quick debugging or create custom formats for production access logs.
Why it exists
Web servers can feel like black boxes. Without explicit logging, you have no visibility into the requests hitting your endpoints, whether they are succeeding, or how long they are taking. Manually adding logging to every route handler is repetitive and error-prone. Morgan was created to provide comprehensive HTTP request logging for an Express application in a single line of code.
The mental model
Think of Morgan as a doorman for your Express application. As each request enters and leaves, the doorman jots down key details in a logbook: who it was (IP address), what they asked for (method and URL), how you responded (status code), and how long the entire exchange took. You can tell the doorman to be concise (the 'tiny' format) or extremely detailed (the 'combined' format), or give them a custom template to follow.
How it works
Morgan is Express middleware, inserted into the request-response chain via app.use(). When a request arrives, it passes through Morgan. By default, Morgan waits until the response is finished, gathers data from both the request (req) and response (res) objects, formats it into a string using tokens like :method and :status, and writes it to a stream, which defaults to process.stdout. You can provide predefined format strings ('combined', 'tiny'), build your own, or even pass a function for total control over the log line.
When to use it
Use Morgan in development for a real-time console feed of requests hitting your server. In production, use it to generate standard Apache-style access logs for monitoring, auditing, or feeding into log analysis systems. It's excellent for debugging by providing a clear record of traffic patterns and specific request details.
When not to use it
Morgan is purpose-built for logging HTTP requests. It is not a general-purpose application logger. For logging business logic, database errors, or other internal application events, use a dedicated logging library like Winston or Pino. Morgan's job is to report on the HTTP traffic, not the internal workings of your code.
One canonical example
A common production pattern is to reduce log noise by only logging failed requests. Morgan's skip option makes this easy. The following configuration uses the detailed 'combined' format but only writes a log line if the HTTP response status code is 400 or greater, indicating a client or server error.
app.use(morgan('combined', {skip: function (req, res) { return res.statusCode < 400 } }));
This keeps your logs clean during normal operation but ensures every error is captured for analysis.
Interview question
Which logging task is NOT a primary use case for Morgan in an Express application?
- a.Filtering and logging only HTTP requests that result in error status codes.
- b.Generating standard access logs for successful API calls in production.
- c.Tracking internal application events like database connection failures.Correct
- d.Recording details of every incoming HTTP request for debugging.
Why? this is the answer
Morgan is purpose-built for logging HTTP requests and responses, providing visibility into traffic patterns. It is explicitly stated that Morgan is not a general-purpose application logger and should not be used for internal application events like database errors, which require dedicated logging libraries.
Just read this? Test yourself on what you have been reading.
Read the original → expressjs.com
- #nodejs
- #express
- #middleware
- #logging
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on nodejs — each one lists the topics its interview covers.
See open roles