Bird
Raised Fist0
Expressframework~20 mins

Morgan for HTTP request logging in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Morgan Logging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Morgan default logging output
What is the output in the console when a GET request is made to '/' using Morgan's default format in an Express app?
Express
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);
A:method :url :status :res[content-length] - :response-time ms
BGET / 404 0 - 0.000 ms
CGET / 200 5 - 1.234 ms
DGET / Hello 200 5 - 1.234 ms
Attempts:
2 left
💡 Hint
Morgan's 'dev' format shows method, url, status, response size, and response time.
❓ Configuration
intermediate
2:00remaining
Custom Morgan token usage
Which Morgan configuration correctly adds a custom token 'user-id' that logs the value of req.user.id?
Express
const morgan = require('morgan');
// Assume req.user.id exists
A
morgan.token('user-id', (req, res) => req.user.id);
app.use(morgan(':method :url :user-id'))
B
morgan.token('user-id', (res) => res.user.id);
app.use(morgan(':method :url :user-id'))
C
morgan.token('user-id', (req) => req.user.id);
app.use(morgan(':method :url :user-id'))
D
morgan.token('user-id', (req, res) => res.user.id);
app.use(morgan(':method :url :user-id'))
Attempts:
2 left
💡 Hint
Morgan tokens receive (req, res) arguments; user info is on req.
❓ Troubleshoot
advanced
2:00remaining
Morgan logs not appearing
An Express app uses Morgan for logging but no logs appear in the console when requests are made. What is the most likely cause?
AMorgan is configured with 'combined' format
BMorgan is used without calling app.use()
CMorgan is not installed via npm
DMorgan middleware is added after route handlers
Attempts:
2 left
💡 Hint
Middleware order matters in Express.
🔀 Workflow
advanced
2:00remaining
Conditional Morgan logging in production
How to configure Morgan to log requests only when NODE_ENV is 'development'?
Express
const morgan = require('morgan');
const express = require('express');
const app = express();
Aapp.use(morgan('dev'));
Bapp.use(morgan('dev', { skip: () => process.env.NODE_ENV !== 'development' }));
Capp.use(morgan('dev')); if (process.env.NODE_ENV !== 'development') { app.disable('morgan'); }
Dif (process.env.NODE_ENV === 'development') { app.use(morgan('dev')); }
Attempts:
2 left
💡 Hint
Morgan supports a skip option to conditionally skip logging.
✅ Best Practice
expert
2:00remaining
Best practice for logging sensitive data with Morgan
Which approach is best to avoid logging sensitive information like authorization headers with Morgan?
ACreate a custom Morgan token that excludes sensitive headers and use it in the format
BUse Morgan's 'combined' format and manually remove sensitive headers from logs later
CDisable Morgan logging entirely in production
DLog all headers but encrypt the log files
Attempts:
2 left
💡 Hint
Custom tokens allow control over what is logged.

Practice

(1/5)
1. What is the main purpose of using morgan in an Express app?
easy
A. To log HTTP requests for monitoring server activity
B. To handle database connections
C. To serve static files
D. To manage user sessions

Solution

  1. Step 1: Understand Morgan's role

    Morgan is a middleware that logs details about HTTP requests to the console or a file.
  2. Step 2: Compare options with Morgan's function

    Only logging HTTP requests matches Morgan's purpose; other options relate to different tasks.
  3. Final Answer:

    To log HTTP requests for monitoring server activity -> Option A
  4. Quick 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
A. app.use(morgan.format('tiny'))
B. app.use(morgan('tiny'))
C. app.use('morgan', 'tiny')
D. app.use(morgan.tiny())

Solution

  1. Step 1: Recall Morgan usage syntax

    Morgan is used as middleware by calling morgan('format') inside app.use().
  2. Step 2: Check each option's syntax

    Only app.use(morgan('tiny')) matches the correct syntax; others are invalid method calls or parameters.
  3. Final Answer:

    app.use(morgan('tiny')) -> Option B
  4. Quick 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
A. GET / 200 5 - 0.123 ms
B. GET / 404 - - ms
C. POST / 200 5 - 0.123 ms
D. Error: Morgan format not found

Solution

  1. Step 1: Identify the HTTP method and route

    The code handles GET requests to '/', so a GET / request is valid and returns 200 status.
  2. 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'.
  3. Final Answer:

    GET / 200 5 - 0.123 ms -> Option A
  4. Quick 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
A. Morgan requires a database connection to log
B. Morgan does not support 'combined' format
C. Morgan middleware is added after route handlers
D. Morgan only logs errors, not requests

Solution

  1. Step 1: Understand middleware order importance

    Express runs middleware in order. If Morgan is added after routes, requests may not pass through it.
  2. 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.
  3. Final Answer:

    Morgan middleware is added after route handlers -> Option C
  4. Quick 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
A. app.use(morgan('combined', { filter: (req, res) => res.statusCode >= 400 }))
B. app.use(morgan('combined', { skip: (req, res) => res.statusCode >= 400 }))
C. app.use(morgan('combined', { only: (req, res) => res.statusCode >= 400 }))
D. app.use(morgan('combined', { skip: (req, res) => res.statusCode < 400 }))

Solution

  1. Step 1: Understand Morgan's skip option

    The 'skip' option is a function that returns true to skip logging that request.
  2. 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.
  3. 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.
  4. Final Answer:

    app.use(morgan('combined', { skip: (req, res) => res.statusCode < 400 })) -> Option D
  5. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard