Status codes and headers
📖 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
AppControllerAdd a GET route handler method named
getHelloSet the HTTP status code to
201 (Created) for the responseAdd a custom header
X-Custom-Header with value NestJSReturn 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