What is the problem with this code when a user is not found?
medium
A. It correctly sends a 404 status with no body.
B. It sends a 404 status but still sends undefined as response body.
C. It throws a syntax error due to missing semicolon.
D. It sends a 500 Internal Server Error automatically.
Solution
Step 1: Analyze response when user not found
res.status(404) sets status but does not end response or send body.
Step 2: Check what res.send(user) does
res.status(404) sets the status, but execution continues to res.send(undefined), which sends the string 'undefined' as body with 404 status.
Final Answer:
It sends a 404 status but still sends undefined as response body. -> Option B
Quick Check:
Missing res.send after status causes wrong response [OK]
Hint: Always send response body or end response after setting status [OK]
Common Mistakes:
Setting status without sending response body
Assuming status alone ends response
Not checking for undefined response body
5. You are designing a REST API for a library. When a client requests a book by ID that does not exist, you want to return a 404 Not Found with a helpful JSON message. Which of the following Express.js code snippets correctly implements this behavior?
hard
A. app.get('/books/:id', (req, res) => {
const books = {1: '1984', 2: 'Brave New World'};
const book = books[req.params.id];
if (!book) {
res.sendStatus(404);
}
res.json({book});
});
B. app.get('/books/:id', (req, res) => {
const books = {1: '1984', 2: 'Brave New World'};
const book = books[req.params.id];
if (!book) {
res.json({error: 'Book not found'});
}
res.status(404).json({book});
});
C. app.get('/books/:id', (req, res) => {
const books = {1: '1984', 2: 'Brave New World'};
const book = books[req.params.id];
if (!book) {
res.status(404);
}
res.json({book});
});
D. app.get('/books/:id', (req, res) => {
const books = {1: '1984', 2: 'Brave New World'};
const book = books[req.params.id];
if (!book) {
res.status(404).json({error: 'Book not found'});
return;
}
res.json({book});
});
Solution
Step 1: Check how 404 and message are sent
app.get('/books/:id', (req, res) => {
const books = {1: '1984', 2: 'Brave New World'};
const book = books[req.params.id];
if (!book) {
res.status(404).json({error: 'Book not found'});
return;
}
res.json({book});
}); sets status 404 and sends JSON with error message, then returns to avoid sending more.
Step 2: Verify other options
app.get('/books/:id', (req, res) => {
const books = {1: '1984', 2: 'Brave New World'};
const book = books[req.params.id];
if (!book) {
res.json({error: 'Book not found'});
}
res.status(404).json({book});
}); sends JSON without status 404 first, then tries to send 404 with book JSON (wrong order). app.get('/books/:id', (req, res) => {
const books = {1: '1984', 2: 'Brave New World'};
const book = books[req.params.id];
if (!book) {
res.status(404);
}
res.json({book});
}); sets status but does not send response properly. app.get('/books/:id', (req, res) => {
const books = {1: '1984', 2: 'Brave New World'};
const book = books[req.params.id];
if (!book) {
res.sendStatus(404);
}
res.json({book});
}); sends 404 status but then sends book JSON anyway.
Final Answer:
Sets status(404).json({error: 'Book not found'}); return; to send 404 with helpful JSON message and end response. -> Option D
Quick Check:
Set status, send JSON, and return to end response = app.get('/books/:id', (req, res) => {
const books = {1: '1984', 2: 'Brave New World'};
const book = books[req.params.id];
if (!book) {
res.status(404).json({error: 'Book not found'});
return;
}
res.json({book});
}); [OK]
Hint: Set status, send JSON error, and return immediately [OK]
Common Mistakes:
Not returning after sending 404 response
Sending response body after status without ending
Mixing order of status and response methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.