Which of the following Express.js code snippets correctly implements this behavior?
hard📝 Application Q15 of 15
Rest API - HTTP Status Codes
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?
Aapp.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});
});
Bapp.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});
});
Capp.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});
});
Dapp.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});
});
Step-by-Step Solution
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]
Quick Trick:Set status, send JSON error, and return immediately [OK]
Common Mistakes:
MISTAKES
Not returning after sending 404 response
Sending response body after status without ending
Mixing order of status and response methods
Master "HTTP Status Codes" in Rest API
9 interactive learning modes - each teaches the same concept differently