Discover how simple status codes and headers can save your app from confusing errors!
Why Status codes and headers in NestJS? - Purpose & Use Cases
Imagine building a web server that must tell browsers if a page loaded successfully or if there was an error, and also send extra info like content type or caching rules.
Manually setting status codes and headers means writing repetitive code everywhere, risking mistakes like sending wrong codes or missing important headers, which confuses browsers and users.
Using NestJS's built-in tools to set status codes and headers makes this easy and consistent, so your server clearly communicates results and extra info without extra hassle.
res.status(200); res.setHeader('Content-Type', 'application/json'); res.send(data);
@Get() @Header('Content-Type', 'application/json') @HttpCode(200) getData() { return data; }
This lets your server clearly tell clients what happened and how to handle the response, improving communication and user experience.
When a user logs in, your server sends a 200 status with a token header; if login fails, it sends a 401 status so the app knows to ask for credentials again.
Manual status and header handling is error-prone and repetitive.
NestJS decorators simplify setting status codes and headers.
Clear communication improves client-server interaction and user experience.
Practice
201 usually indicate in a response?Solution
Step 1: Understand HTTP status codes
HTTP status codes communicate the result of a client's request. Code 201 means "Created".Step 2: Match 201 with its meaning
201 specifically means a new resource was successfully created on the server.Final Answer:
A new resource was successfully created -> Option AQuick Check:
201 = Created [OK]
- Confusing 201 with 404 (not found)
- Thinking 201 means server error
- Mixing 201 with 400 (bad request)
X-Custom-Header with value 123 in a NestJS controller using Express response?Solution
Step 1: Recall Express response methods
Express response object usesres.set()to set headers.Step 2: Check method and value types
res.set()accepts header name and string value. res.set('X-Custom-Header', '123'); uses correct method and string value.Final Answer:
res.set('X-Custom-Header', '123'); -> Option BQuick Check:
Use res.set() to set headers [OK]
- Using res.setHeader (not Express method)
- Passing number instead of string as header value
- Using non-existent res.addHeader method
async getData(@Res() res) {
res.status(204).set('X-Info', 'No Content').send();
}Solution
Step 1: Analyze status and header setting
The code sets status to 204 and header 'X-Info' to 'No Content'.Step 2: Understand 204 status behavior
Status 204 means "No Content" and allows custom headers. The header is set correctly.Final Answer:
Status 204 with header X-Info: No Content -> Option CQuick Check:
Status 204 + header set = Status 204 with header X-Info: No Content [OK]
- Assuming 204 means no headers allowed
- Confusing default status 200 with explicit 204
- Ignoring header set after status
async update(@Res() res) {
res.status(200);
res.set('Content-Type', 'application/json');
res.send({ success: true });
res.status(201);
}Solution
Step 1: Review response flow
res.send() sends the response and ends it. Any code after it won't affect response.Step 2: Check status calls order
Calling res.status(201) after res.send() does nothing because response is already sent.Final Answer:
Calling res.status(201) after res.send() has no effect -> Option AQuick Check:
res.send() ends response, later status calls ignored [OK]
- Trying to set status after sending response
- Thinking res.send() can't send JSON
- Believing res.set() must come before res.status()
X-Request-ID and a JSON body in NestJS. Which code snippet correctly achieves this?Solution
Step 1: Order of method calls matters
In Express, chaining status, set, and json works if status is set before sending.Step 2: Check each option's correctness
Options A and C call send before completing status/header setup, invalid. res.set('X-Request-ID', requestId).status(202).send('Accepted'); sets header and status but uses res.send('Accepted') sending plain text instead of JSON object. res.status(202).set('X-Request-ID', requestId).json({ message: 'Accepted' }); correctly sets status, header, sends JSON.Step 3: Confirm best practice
res.status(202).set('X-Request-ID', requestId).json({ message: 'Accepted' }); is the clearest and most common pattern: status, set header, then send JSON.Final Answer:
res.status(202).set('X-Request-ID', requestId).json({ message: 'Accepted' }); -> Option DQuick Check:
Status then headers then send JSON [OK]
- Calling send before setting status or headers
- Using send instead of json for JSON body
- Setting headers after sending response
Ready to prove it?
Pass the test and this pattern moves unknown
