Morgan is a middleware for Express.js that logs HTTP requests to the console or a file, helping developers see details about incoming requests.
Click to reveal answer
beginner
How do you install Morgan in an Express project?
You install Morgan using npm with the command: npm install morgan.
Click to reveal answer
beginner
How do you use Morgan to log all HTTP requests in Express?
You add Morgan as middleware in your Express app like this:<br><pre>const morgan = require('morgan');
app.use(morgan('dev'));</pre><br>This logs requests in a concise colored format.
Click to reveal answer
intermediate
What does the 'dev' format in Morgan do?
The 'dev' format logs HTTP method, URL, status code, response time, and response length in a colored, easy-to-read style.
Click to reveal answer
intermediate
How can you log HTTP requests to a file using Morgan?
You create a write stream and pass it to Morgan like this:<br><pre>const fs = require('fs');
const path = require('path');
const accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' });
app.use(morgan('combined', { stream: accessLogStream }));</pre><br>This saves logs in Apache combined format to 'access.log'.
Click to reveal answer
What is the primary purpose of Morgan in an Express app?
ATo log HTTP requests
BTo handle database connections
CTo serve static files
DTo manage user sessions
✗ Incorrect
Morgan is used to log HTTP requests for monitoring and debugging.
Which command installs Morgan in a Node.js project?
Anpm install morgan
Bnpm install logger
Cnpm install express
Dnpm install http
✗ Incorrect
Morgan is installed with npm install morgan.
What does the 'combined' format in Morgan include?
AOnly HTTP method and URL
BOnly IP address and date
COnly status code and response time
DHTTP method, URL, status, response time, and user agent
✗ Incorrect
The 'combined' format logs detailed info including user agent.
How do you add Morgan middleware to an Express app?
Aapp.get(morgan)
Bapp.use(morgan('dev'))
Capp.listen(morgan)
Dapp.post(morgan)
✗ Incorrect
Morgan is added as middleware using app.use(morgan('dev')).
Which Node.js module is used to create a write stream for logging Morgan output to a file?
Ahttp
Burl
Cfs
Dpath
✗ Incorrect
The 'fs' module creates write streams for file operations.
Explain how to set up Morgan in an Express app to log HTTP requests to the console.
Think about npm install, require, and middleware usage.
You got /4 concepts.
Describe how to configure Morgan to save HTTP request logs to a file instead of the console.
Consider how to write logs to a file using Node.js modules.
You got /4 concepts.
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
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 A
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
Step 1: Recall Morgan usage syntax
Morgan is used as middleware by calling morgan('format') inside app.use().
Step 2: Check each option's syntax
Only app.use(morgan('tiny')) matches the correct syntax; others are invalid method calls or parameters.
Final Answer:
app.use(morgan('tiny')) -> Option B
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 '/'?
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.