📖 Scenario: You are building a simple Express server that logs the HTTP method and URL of incoming requests. This helps you understand what requests your server receives.
🎯 Goal: Create an Express server that listens on port 3000 and logs the req.method and req.url for every request.
📋 What You'll Learn
Create an Express app using express()
Use app.use to add a middleware that logs req.method and req.url
Start the server listening on port 3000
Use exact variable names: app, req, res, next
💡 Why This Matters
🌍 Real World
Logging request methods and URLs helps developers monitor and debug web servers by seeing what requests come in.
💼 Career
Understanding req.method and req.url is essential for backend developers working with Express to handle routing and middleware.
Progress0 / 4 steps
1
Setup Express app
Create a variable called express that requires the 'express' module. Then create a variable called app by calling express().
Express
Hint
Use require('express') to import Express and then call it to create the app.
2
Add logging middleware
Use app.use to add a middleware function with parameters req, res, and next. Inside it, write a line that logs req.method and req.url using console.log. Then call next().
Express
Hint
The middleware function receives req, res, and next. Use console.log(req.method, req.url) to log the method and URL.
3
Add a simple route
Add a GET route for path '/' using app.get. The handler function should take req and res and respond with res.send('Hello World').
Express
Hint
Use app.get to create a route for '/' that sends back 'Hello World'.
4
Start the server
Use app.listen to start the server on port 3000. The callback function should log 'Server running on port 3000'.
Express
Hint
Use app.listen(3000, () => { console.log(...) }) to start the server and log a message.
Practice
(1/5)
1. What does req.method represent in an Express request?
easy
A. The HTTP method used by the client (e.g., GET, POST)
B. The full URL including domain name
C. The IP address of the client
D. The server's response status code
Solution
Step 1: Understand the purpose of req.method
req.method holds the HTTP method used by the client to make the request, such as GET, POST, PUT, DELETE.
Step 2: Differentiate from other request properties
req.url gives the path and query, IP address is separate, and response status is not part of the request object.
Final Answer:
The HTTP method used by the client (e.g., GET, POST) -> Option A
Quick Check:
req.method = HTTP method [OK]
Hint: Remember: method means action type like GET or POST [OK]
Common Mistakes:
Confusing req.method with req.url
Thinking req.method gives client IP
Assuming req.method is server response code
2. Which of the following is the correct way to check if a request is a POST method in Express?
easy
A. if (req.url === 'POST')
B. if (req.url === '/post')
C. if (req.method = 'POST')
D. if (req.method === 'POST')
Solution
Step 1: Identify the property holding HTTP method
The HTTP method is stored in req.method, so we compare it to the string 'POST'.
Step 2: Check syntax correctness
if (req.method === 'POST') uses strict equality (===) to compare req.method to 'POST', which is correct. if (req.method = 'POST') uses assignment (=) which is wrong.
Final Answer:
if (req.method === 'POST') -> Option D
Quick Check:
Check req.method with === 'POST' [OK]
Hint: Use === to compare req.method with 'POST' string [OK]
Common Mistakes:
Using assignment (=) instead of comparison (===)
Checking req.url instead of req.method
Comparing req.method to lowercase 'post'
3. Given this Express code snippet, what will be logged if a client sends a GET request to /home?user=123?
B. Using assignment (=) instead of comparison (===) in the if condition
C. Logging message is incorrect
D. next() is called before the if condition
Solution
Step 1: Check the if condition syntax
The condition uses = which assigns 'POST' to req.method instead of comparing it. This causes a bug.
Step 2: Identify correct comparison operator
Use === to compare values in JavaScript, so the condition should be req.method === 'POST'.
Final Answer:
Using assignment (=) instead of comparison (===) in the if condition -> Option B
Quick Check:
Use === for comparison, not = [OK]
Hint: Use === to compare, not = which assigns [OK]
Common Mistakes:
Using = instead of === in if condition
Thinking next() must be before if
Assuming parentheses needed around req.method
5. You want to create an Express middleware that logs a message only when the client requests the URL /api/data with a GET method. Which code snippet correctly implements this?
hard
A. app.use((req, res, next) => {
if (req.method === 'POST' && req.url === '/api/data') {
console.log('GET request to /api/data');
}
next();
});
B. app.use((req, res, next) => {
if (req.method = 'GET' || req.url === '/api/data') {
console.log('GET request to /api/data');
}
next();
});
C. app.use((req, res, next) => {
if (req.method === 'GET' && req.url === '/api/data') {
console.log('GET request to /api/data');
}
next();
});
D. app.use((req, res, next) => {
if (req.method === 'GET' && req.url === '/api') {
console.log('GET request to /api/data');
}
next();
});
Solution
Step 1: Check method and URL conditions
The middleware should check if req.method is exactly 'GET' and req.url is exactly '/api/data'.
Step 2: Verify logical operators and syntax
app.use((req, res, next) => {
if (req.method === 'GET' && req.url === '/api/data') {
console.log('GET request to /api/data');
}
next();
}); uses strict equality (===) and logical AND (&&) correctly. app.use((req, res, next) => {
if (req.method = 'GET' || req.url === '/api/data') {
console.log('GET request to /api/data');
}
next();
}); uses assignment (=) and OR (||) which is wrong. Options C and D check wrong method or URL.
Final Answer:
app.use((req, res, next) => {
if (req.method === 'GET' && req.url === '/api/data') {
console.log('GET request to /api/data');
}
next();
}); -> Option C
Quick Check:
Check req.method === 'GET' AND req.url === '/api/data' [OK]
Hint: Use === and && to check method and exact URL [OK]
Common Mistakes:
Using = instead of ===
Using || instead of &&
Checking wrong URL or method
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.