Morgan helps you see details about each web request your server gets. It makes it easy to watch what happens in your app.
Morgan for HTTP request logging in Express
Introduction
Syntax
Express
const morgan = require('morgan'); app.use(morgan('tiny'));
Use require('morgan') to import Morgan.
Use app.use(morgan('format')) to add logging middleware.
Examples
Express
app.use(morgan('combined'));Express
app.use(morgan('tiny'));Express
app.use(morgan(':method :url :status :res[content-length] - :response-time ms'));Sample Program
This Express app uses Morgan with the 'tiny' format to log each request in the console.
Express
const express = require('express'); const morgan = require('morgan'); const app = express(); app.use(morgan('tiny')); app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Important Notes
You can choose different formats like 'combined', 'common', 'dev', 'short', and 'tiny'.
Logs appear in the console by default but can be saved to files with extra setup.
Morgan runs before your routes, so it logs every request your server gets.
Summary
Morgan logs HTTP requests to help you see what your server is doing.
Use app.use(morgan('format')) to add logging easily.
Pick a format that fits how much detail you want to see.
Practice
1. What is the main purpose of using
morgan in an Express app?easy
Solution
Step 1: Understand Morgan's role
Morgan is a middleware that logs details about HTTP requests to the console or a file.Step 2: Compare options with Morgan's function
Only logging HTTP requests matches Morgan's purpose; other options relate to different tasks.Final Answer:
To log HTTP requests for monitoring server activity -> Option AQuick Check:
Logging HTTP requests = A [OK]
Hint: Morgan logs requests, not database or sessions [OK]
Common Mistakes:
- Confusing Morgan with session or static file middleware
- Thinking Morgan manages database connections
- Assuming Morgan serves files
2. Which of the following is the correct way to add Morgan middleware with the 'tiny' format in an Express app?
easy
Solution
Step 1: Recall Morgan usage syntax
Morgan is used as middleware by callingmorgan('format')insideapp.use().Step 2: Check each option's syntax
Onlyapp.use(morgan('tiny'))matches the correct syntax; others are invalid method calls or parameters.Final Answer:
app.use(morgan('tiny')) -> Option BQuick Check:
Use morgan('format') inside app.use() = D [OK]
Hint: Use morgan('format') inside app.use() exactly [OK]
Common Mistakes:
- Calling morgan as a method like morgan.tiny()
- Passing strings directly to app.use() without morgan()
- Using non-existent morgan.format() method
3. Given this Express code snippet, what will Morgan log when a GET request is made to '/'?
const express = require('express');
const morgan = require('morgan');
const app = express();
app.use(morgan('dev'));
app.get('/', (req, res) => res.send('Hello'));
app.listen(3000);medium
Solution
Step 1: Identify the HTTP method and route
The code handles GET requests to '/', so a GET / request is valid and returns 200 status.Step 2: Understand Morgan 'dev' format output
The 'dev' format logs method, URL, status code, response time, and response size roughly like 'GET / 200 5 - 0.123 ms'.Final Answer:
GET / 200 5 - 0.123 ms -> Option AQuick Check:
GET / with 200 status logs as in B [OK]
Hint: GET '/' with 'dev' logs method, URL, status, time [OK]
Common Mistakes:
- Confusing POST with GET method
- Assuming 404 status for existing route
- Expecting error from Morgan format
4. You added Morgan to your Express app with
app.use(morgan('combined')), but no logs appear in the console. What is the most likely cause?medium
Solution
Step 1: Understand middleware order importance
Express runs middleware in order. If Morgan is added after routes, requests may not pass through it.Step 2: Check Morgan format and behavior
'combined' is a valid format; Morgan logs all requests, not just errors; it does not need a database.Final Answer:
Morgan middleware is added after route handlers -> Option CQuick Check:
Middleware order matters; Morgan must be before routes [OK]
Hint: Add Morgan before routes to see logs [OK]
Common Mistakes:
- Thinking 'combined' format is invalid
- Believing Morgan logs only errors
- Assuming Morgan needs a database
5. You want to log HTTP requests only if the response status is 400 or higher using Morgan. Which code snippet correctly achieves this?
hard
Solution
Step 1: Understand Morgan's skip option
The 'skip' option is a function that returns true to skip logging that request.Step 2: Apply condition to skip requests with status below 400
To log only 400 or higher, skip requests where statusCode is less than 400.Step 3: Verify other options
'filter' and 'only' are not valid Morgan options; app.use(morgan('combined', { skip: (req, res) => res.statusCode >= 400 })) skips 400+ which is opposite of desired.Final Answer:
app.use(morgan('combined', { skip: (req, res) => res.statusCode < 400 })) -> Option DQuick Check:
Skip if status < 400 to log 400+ [OK]
Hint: Use skip to ignore status below 400 [OK]
Common Mistakes:
- Using skip condition backwards
- Using invalid options like filter or only
- Confusing skip with logging condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
