Challenge - 5 Problems
Morgan Logging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2: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);
Attempts:
2 left
💡 Hint
Morgan's 'dev' format shows method, url, status, response size, and response time.
✗ Incorrect
Morgan's 'dev' format logs the HTTP method, URL, status code, response content length, and response time in milliseconds.
❓ Configuration
intermediate2: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
Attempts:
2 left
💡 Hint
Morgan tokens receive (req, res) arguments; user info is on req.
✗ Incorrect
Morgan tokens are functions with (req, res) parameters. To access user id, use req.user.id.
❓ Troubleshoot
advanced2: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?
Attempts:
2 left
💡 Hint
Middleware order matters in Express.
✗ Incorrect
Morgan must be added before routes to log requests. If added after, requests are handled before logging.
🔀 Workflow
advanced2: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();
Attempts:
2 left
💡 Hint
Morgan supports a skip option to conditionally skip logging.
✗ Incorrect
Using the skip option with a function allows Morgan to skip logging when not in development.
✅ Best Practice
expert2:00remaining
Best practice for logging sensitive data with Morgan
Which approach is best to avoid logging sensitive information like authorization headers with Morgan?
Attempts:
2 left
💡 Hint
Custom tokens allow control over what is logged.
✗ Incorrect
Creating a custom token to exclude sensitive data ensures logs do not contain secrets while still logging useful info.