If a client sends a request with header Content-Type: application/json, what will be the output?
medium
A. application/json
B. content-type
C. undefined
D. Error
Solution
Step 1: Understand how headers are accessed
Headers in Node.js request object are stored in lowercase keys inside req.headers. So content-type is the correct key.
Step 2: Check the code output
The code returns req.headers['content-type'], which matches the header sent by the client: application/json.
Final Answer:
application/json -> Option A
Quick Check:
Header content-type value = application/json [OK]
Hint: Headers keys are lowercase in req.headers [OK]
Common Mistakes:
Using 'Content-Type' instead of 'content-type' key
Expecting req.headers to be case-sensitive
Assuming undefined if header not found
4. What is wrong with this Node.js code snippet that tries to read JSON data from the request body?
const http = require('http');
const server = http.createServer((req, res) => {
const data = req.body;
res.end(data);
});
server.listen(3000);
medium
A. res.end cannot send data
B. req.body is undefined without parsing the data
C. req.body is a function, not a property
D. Missing require for http module
Solution
Step 1: Understand how request body works in Node.js
In Node.js core, req.body is not automatically populated. You must collect data chunks and parse them manually or use middleware.
Step 2: Identify the error in the code
The code tries to access req.body directly, which will be undefined, causing the response to send undefined.
Final Answer:
req.body is undefined without parsing the data -> Option B
Quick Check:
req.body needs manual parsing [OK]
Hint: req.body is empty unless parsed manually or with middleware [OK]
Common Mistakes:
Assuming req.body is auto-filled in Node.js core
Trying to send undefined data without error
Confusing res.end usage
5. You want to log the client's IP address and the requested URL path in a Node.js HTTP server. Which code snippet correctly accesses these properties from the request object?
hard
A. console.log(req.connection.remoteAddress, req.path);
B. console.log(req.ip, req.path);
C. console.log(req.address, req.route);
D. console.log(req.socket.remoteAddress, req.url);
Solution
Step 1: Identify how to get client IP in Node.js
The client IP is accessible via req.socket.remoteAddress or req.connection.remoteAddress. The req.socket is the modern standard.
Step 2: Identify how to get URL path
The URL path is stored in req.url. Other options like req.path or req.route are not standard in Node.js core.
Step 3: Compare options
console.log(req.socket.remoteAddress, req.url); uses req.socket.remoteAddress and req.url, which is correct and modern.
Final Answer:
console.log(req.socket.remoteAddress, req.url); -> Option D
Quick Check:
IP = req.socket.remoteAddress, URL = req.url [OK]
Hint: Use req.socket.remoteAddress and req.url for IP and path [OK]
Common Mistakes:
Using req.ip which is Express-specific
Using req.path which is not in Node.js core
Trying to access req.address or req.route which don't exist