0
0
Expressframework~20 mins

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

Choose your learning style9 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.