0
0
NestJSframework~30 mins

Response handling in NestJS - Mini Project: Build & Apply

Choose your learning style9 modes available
Response Handling in NestJS
📖 Scenario: You are building a simple NestJS API that returns user data. You want to control how the response is sent back to the client.
🎯 Goal: Create a NestJS controller that handles a GET request and returns a JSON response with a status code and a message.
📋 What You'll Learn
Create a controller named UsersController
Add a GET route handler method named getUser
Return a JSON object with statusCode and message fields
Use NestJS @Res() decorator to send the response with status 200
💡 Why This Matters
🌍 Real World
APIs often need to send custom responses with specific status codes and messages to clients.
💼 Career
Understanding response handling in NestJS is essential for backend developers building RESTful APIs.
Progress0 / 4 steps
1
Create UsersController with a getUser method
Create a class called UsersController and add a method named getUser inside it.
NestJS
Need a hint?

Use export class UsersController {} and define getUser() inside.

2
Add @Controller and @Get decorators
Add the @Controller('users') decorator above the UsersController class and the @Get() decorator above the getUser method.
NestJS
Need a hint?

Import and use @Controller('users') and @Get() decorators.

3
Inject Response object and send JSON response
Add a parameter named res to getUser method with the @Res() decorator. Use res.status(200).json() to send a JSON response with { statusCode: 200, message: 'User data fetched successfully' }.
NestJS
Need a hint?

Use @Res() to inject the response object and send JSON with res.status(200).json(...).

4
Import necessary decorators from @nestjs/common
Add import statements for Controller, Get, and Res from @nestjs/common at the top of the file.
NestJS
Need a hint?

Import Controller, Get, and Res from @nestjs/common.