📖 Scenario: You are building a simple NestJS API that responds to client requests with proper HTTP status codes and custom headers. This helps clients understand the result of their requests and receive extra information.
🎯 Goal: Create a NestJS controller with one GET endpoint that returns a JSON message. Set a custom HTTP status code and add a custom header to the response.
📋 What You'll Learn
Create a controller named AppController
Add a GET route handler method named getHello
Set the HTTP status code to 201 (Created) for the response
Add a custom header X-Custom-Header with value NestJS
Return a JSON object with a message property set to 'Hello World'
💡 Why This Matters
🌍 Real World
APIs often need to send specific status codes and headers to inform clients about the result of their requests and provide extra metadata.
💼 Career
Understanding how to control HTTP status codes and headers is essential for backend developers building RESTful APIs with NestJS.
Progress0 / 4 steps
1
Create the controller and GET method
Create a NestJS controller class named AppController. Inside it, add a method named getHello decorated with @Get() that returns an object { message: 'Hello World' }.
NestJS
Hint
Use @Controller() to define the controller and @Get() to define the GET route handler.
2
Add HTTP status code 201
Import @HttpCode from @nestjs/common. Add the decorator @HttpCode(201) above the getHello method to set the HTTP status code to 201.
NestJS
Hint
The @HttpCode() decorator sets the response status code.
3
Inject response object to set headers
Import Res and Response from @nestjs/common and express respectively. Modify the getHello method to accept a parameter res decorated with @Res() and typed as Response. Use res.setHeader('X-Custom-Header', 'NestJS') to add the custom header. Then send the JSON response using res.status(201).json({ message: 'Hello World' }).
NestJS
Hint
Use the @Res() decorator to get the response object and set headers before sending the response.
4
Complete the controller with proper imports
Ensure the imports include Controller, Get, HttpCode, and Res from @nestjs/common, and Response from express. The AppController class should have the getHello method as defined with the custom header and status code.
NestJS
Hint
Check that all imports and method code are correct and complete.
Practice
(1/5)
1. In NestJS, what does the HTTP status code 201 usually indicate in a response?
easy
A. A new resource was successfully created
B. The request was malformed or invalid
C. The requested resource was not found
D. The server encountered an internal error
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 A
Quick Check:
201 = Created [OK]
Hint: 201 means resource created successfully [OK]
Common Mistakes:
Confusing 201 with 404 (not found)
Thinking 201 means server error
Mixing 201 with 400 (bad request)
2. Which of the following is the correct way to set a custom header X-Custom-Header with value 123 in a NestJS controller using Express response?
easy
A. res.setHeader('X-Custom-Header', 789);
B. res.set('X-Custom-Header', '123');
C. res.header('X-Custom-Header', '789');
D. res.addHeader('X-Custom-Header', '123');
Solution
Step 1: Recall Express response methods
Express response object uses res.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 B
Quick Check:
Use res.set() to set headers [OK]
Hint: Use res.set() to set headers in NestJS [OK]
Common Mistakes:
Using res.setHeader (not Express method)
Passing number instead of string as header value
Using non-existent res.addHeader method
3. What will be the HTTP status code and header in the response when this NestJS controller method runs?
A. Calling res.status(201) after res.send() has no effect
B. res.set() must be called before res.status()
C. res.send() cannot send JSON objects
D. res.status() cannot be called twice
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 A
Quick Check:
res.send() ends response, later status calls ignored [OK]
Hint: Set status before res.send(), not after [OK]
Common Mistakes:
Trying to set status after sending response
Thinking res.send() can't send JSON
Believing res.set() must come before res.status()
5. You want to send a 202 Accepted status with a custom header X-Request-ID and a JSON body in NestJS. Which code snippet correctly achieves this?
hard
A. res.send({ message: 'Accepted' }).status(202).set('X-Request-ID', requestId);
B. res.set('X-Request-ID', requestId).status(202).send('Accepted');
C. res.status(202).send({ message: 'Accepted' }).set('X-Request-ID', requestId);
D. res.status(202).set('X-Request-ID', requestId).json({ message: 'Accepted' });
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 D
Quick Check:
Status then headers then send JSON [OK]
Hint: Chain status, set, then send/json in that order [OK]
Common Mistakes:
Calling send before setting status or headers
Using send instead of json for JSON body
Setting headers after sending response
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.