Discover how simple decorators can save you hours of messy code and bugs!
Why Route handlers (GET, POST, PUT, DELETE) in NestJS? - Purpose & Use Cases
Imagine building a web app where you manually check the URL and HTTP method for every request to decide what to do.
You write long if-else chains to handle GET, POST, PUT, and DELETE requests all mixed together.
This manual approach quickly becomes messy and hard to maintain.
It's easy to make mistakes, forget to handle some methods, or mix logic for different actions.
Debugging and adding new routes takes a lot of time and effort.
Route handlers in NestJS let you clearly separate logic for GET, POST, PUT, and DELETE requests using simple decorators.
This keeps your code organized, easy to read, and maintain.
Each handler focuses on one task, making your app more reliable and scalable.
if (req.method === 'GET' && req.url === '/items') { /* get items */ } else if (req.method === 'POST' && req.url === '/items') { /* add item */ }
@Get('/items') getItems() { /* get items */ } @Post('/items') addItem() { /* add item */ }
You can build clean, maintainable APIs that clearly separate different actions by HTTP method.
Think of an online store backend where GET fetches products, POST adds new products, PUT updates product details, and DELETE removes products.
Manual request handling is confusing and error-prone.
Route handlers organize code by HTTP methods with clear decorators.
This makes APIs easier to build, read, and maintain.
Practice
Solution
Step 1: Understand HTTP methods and their decorators
In NestJS, each HTTP method has a specific decorator: @Get for GET, @Post for POST, @Put for PUT, and @Delete for DELETE.Step 2: Match the decorator to the GET method
The decorator @Get is specifically designed to handle HTTP GET requests.Final Answer:
@Get -> Option AQuick Check:
GET method uses @Get decorator [OK]
- Confusing @Post or @Put with @Get
- Using @Delete for GET requests
- Not using any decorator
Solution
Step 1: Check the decorator syntax
Decorators in NestJS start with '@' and are case-sensitive. The correct decorator for POST is '@Post'.Step 2: Verify the route path and method name
The route path '/create' is correctly passed as a string argument, and the method name is valid.Final Answer:
@Post('/create') createItem() {} -> Option AQuick Check:
Correct decorator syntax uses '@Post' with parentheses [OK]
- Missing '@' before decorator
- Using lowercase '@post' instead of '@Post'
- Using wrong decorator like '@Put' for POST
@Controller('users')
export class UserController {
@Put(':id')
updateUser() {
return 'User updated';
}
}Solution
Step 1: Identify the controller base path
The @Controller('users') decorator sets the base route path to '/users'.Step 2: Identify the route handler method and path
The @Put(':id') decorator means this handler responds to HTTP PUT requests at the path '/users/:id', where ':id' is a route parameter.Final Answer:
PUT /users/:id -> Option CQuick Check:
@Put with ':id' means PUT method at /users/:id [OK]
- Confusing @Put with @Get or @Post
- Ignoring the controller base path
- Misreading ':id' as a literal string
@Controller('items')
export class ItemController {
@Delete('/remove')
removeItem() {
console.log('Item removed');
}
}Solution
Step 1: Check the decorator and path usage
@Delete can have a path argument like '/remove', so this is valid.Step 2: Check the method implementation
The method only logs to console but does not return any response. HTTP handlers should return a response to the client.Final Answer:
The method should return a value, not just console.log -> Option DQuick Check:
Route handlers must return a response [OK]
- Assuming console.log sends response
- Thinking decorator cannot have path
- Believing method name must match decorator
Solution
Step 1: Use the correct HTTP method and route parameter
Updating a resource uses HTTP PUT, so @Put(':id') is correct to capture the ID as a route parameter.Step 2: Use parameter decorators to access route and body data
@Param('id') extracts the ID from the route, and @Body() extracts the request body data. Both are required for this handler.Step 3: Check method signature and return
The method correctly returns a string confirming the update with the ID.Final Answer:
@Put(':id') updateProduct(@Param('id') id: string, @Body() data: any) { return `Updated product ${id}`; } -> Option BQuick Check:
Use @Put with ':id' and @Param plus @Body [OK]
- Missing route parameter in @Put decorator
- Not using @Param or @Body decorators
- Using wrong HTTP method like @Post for update
Ready to prove it?
Pass the test and this pattern moves unknown
