📖 Scenario: You are building a simple web server using Node.js and Express. You want to add a third-party middleware to log details of every request your server receives. This helps you see what requests come in, like checking a guestbook to know who visited.
🎯 Goal: Build a Node.js Express server that uses the morgan middleware to log HTTP requests to the console.
📋 What You'll Learn
Create an Express app instance
Install and import the morgan middleware
Configure morgan to log requests using the 'tiny' format
Start the server listening on port 3000
💡 Why This Matters
🌍 Real World
Logging HTTP requests helps developers monitor and debug web servers by showing who accessed what and when.
💼 Career
Using third-party middleware like Morgan is a common task in backend development jobs to add features quickly and reliably.
Progress0 / 4 steps
1
Create Express app instance
Write code to import express and create an Express app instance called app.
Node.js
Hint
Use require('express') to import Express and then call express() to create the app.
2
Import Morgan middleware
Add code to import the morgan middleware using require('morgan') and save it in a variable called morgan.
Node.js
Hint
Use const morgan = require('morgan'); to import Morgan.
3
Use Morgan middleware in app
Configure the Express app app to use morgan middleware with the 'tiny' format by calling app.use(morgan('tiny')).
Node.js
Hint
Use app.use() to add Morgan middleware with the 'tiny' logging format.
4
Start server on port 3000
Add code to start the Express app listening on port 3000 using app.listen(3000).
Node.js
Hint
Use app.listen(3000) to start the server on port 3000.
Practice
(1/5)
1. What is the main purpose of using third-party middleware in a Node.js Express app?
easy
A. To replace the Express framework completely
B. To add extra features without writing them from scratch
C. To write all server code manually
D. To make the app run without Node.js
Solution
Step 1: Understand middleware role
Middleware adds features like logging or parsing to an app without extra coding.
Step 2: Compare options
Only To add extra features without writing them from scratch correctly describes middleware's purpose; others are incorrect or unrelated.
Final Answer:
To add extra features without writing them from scratch -> Option B
Quick Check:
Middleware adds features [OK]
Hint: Middleware adds features without extra coding [OK]
Common Mistakes:
Thinking middleware replaces Express
Believing middleware runs without Node.js
Confusing middleware with full app code
2. Which of the following is the correct way to add a third-party middleware called helmet to an Express app?
easy
A. use.app(helmet);
B. app.helmet();
C. helmet(app);
D. app.use(helmet());
Solution
Step 1: Recall Express middleware syntax
Middleware is added using app.use() with the middleware function called.
Step 2: Check options
Only app.use(helmet()); uses app.use(helmet()); correctly; others have wrong syntax.
Final Answer:
app.use(helmet()); -> Option D
Quick Check:
Middleware added with app.use() [OK]
Hint: Use app.use() with middleware function call [OK]
Common Mistakes:
Forgetting parentheses after middleware name
Using incorrect method names like app.helmet()
Passing middleware without calling it
3. Given this code snippet, what will be the output when a GET request is made to '/'?
Missing parentheses when calling cors middleware -> Option A
Quick Check:
Middleware must be called with () [OK]
Hint: Always call middleware as a function with () [OK]
Common Mistakes:
Passing middleware reference without ()
Assuming app.get() lacks callback
Forgetting to specify port in listen
5. You want to add both helmet for security and express.json() for parsing JSON bodies in your Express app. Which code correctly adds both middlewares in the right order?
hard
A. app.use(helmet()); app.use(express.json());
B. app.use(express.json); app.use(helmet);
C. app.use(helmet); app.use(express.json);
D. app.use(express.json()); app.use(helmet());
Solution
Step 1: Understand middleware order importance
Security middleware like helmet should run before body parsing for best protection.
Step 2: Check middleware calls
Both must be called as functions with parentheses; options B and C miss these, A has them but wrong order.
Step 3: Confirm correct order and syntax
app.use(helmet()); app.use(express.json()); calls helmet first, then express.json(), both correctly called.
Final Answer:
app.use(helmet()); app.use(express.json()); -> Option A
Quick Check:
Helmet before json, both called [OK]
Hint: Call middleware with () and order security before parsing [OK]
Common Mistakes:
Not calling middleware as functions
Placing parsing before security middleware
Mixing up middleware order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.