📖 Scenario: You are building a simple Express web server. You want to log every HTTP request your server receives to help monitor traffic and debug issues.
🎯 Goal: Set up the morgan middleware in an Express app to log HTTP requests in the 'tiny' format.
📋 What You'll Learn
Create an Express app instance called app
Import and use the morgan middleware with the 'tiny' format
Add a basic route / that sends 'Hello World!'
Start the server on port 3000
💡 Why This Matters
🌍 Real World
Logging HTTP requests helps developers monitor traffic and troubleshoot issues in web servers.
💼 Career
Understanding middleware like Morgan is essential for backend developers and DevOps engineers to maintain and debug web applications.
Progress0 / 4 steps
1
Create the Express app
Import express and create an Express app instance called app.
Express
Hint
Use const app = express(); to create the app.
2
Import and configure Morgan middleware
Import morgan and use it as middleware in app with the 'tiny' format.
Express
Hint
Use app.use(morgan('tiny')); to add logging.
3
Add a basic route
Add a GET route for '/' that sends the text 'Hello World!' as the response.
Express
Hint
Use app.get('/', (req, res) => { res.send('Hello World!'); });
4
Start the server
Start the Express server on port 3000 and print 'Server running on port 3000' when it starts.
Express
Hint
Use app.listen(3000, () => { console.log('Server running on port 3000'); });
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.