0
0
NestJSframework~10 mins

Route handlers (GET, POST, PUT, DELETE) in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Route handlers (GET, POST, PUT, DELETE)
Client sends HTTP request
NestJS Router receives request
Match route path and HTTP method
Call corresponding handler method
Handler processes data and returns response
Send HTTP response back to client
This flow shows how NestJS receives an HTTP request, matches it to a route handler by path and method, executes the handler, and sends back a response.
Execution Sample
NestJS
import { Controller, Get, Post, Put, Delete, Param } from '@nestjs/common';

@Controller('items')
export class ItemsController {
  @Get() getAll() { return 'Get all items'; }
  @Post() create() { return 'Create item'; }
  @Put(':id') update(@Param('id') id: string) { return `Update item ${id}`; }
  @Delete(':id') remove(@Param('id') id: string) { return `Delete item ${id}`; }
}
Defines route handlers for GET, POST, PUT, DELETE on 'items' path in a NestJS controller.
Execution Table
StepHTTP RequestRoute MatchedHandler CalledResponse Sent
1GET /itemsGET /itemsgetAll()'Get all items'
2POST /itemsPOST /itemscreate()'Create item'
3PUT /items/5PUT /items/:idupdate()'Update item'
4DELETE /items/5DELETE /items/:idremove()'Delete item'
5GET /unknownNo matchNone404 Not Found
💡 Execution stops when no matching route handler is found or response is sent.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
HTTP RequestNoneGET /itemsPOST /itemsPUT /items/5DELETE /items/5GET /unknown
Route MatchedNoneGET /itemsPOST /itemsPUT /items/:idDELETE /items/:idNone
Handler CalledNonegetAll()create()update()remove()None
Response SentNone'Get all items''Create item''Update item''Delete item''404 Not Found'
Key Moments - 3 Insights
Why does the PUT and DELETE handlers include ':id' in the route?
The ':id' is a route parameter that captures a value from the URL, allowing the handler to know which item to update or delete. This is shown in steps 3 and 4 where the route matched includes ':id'.
What happens if a request does not match any route handler?
As shown in step 5, if no route matches the request path and method, NestJS returns a 404 Not Found response and no handler is called.
Can multiple handlers respond to the same path with different HTTP methods?
Yes, as seen in steps 1 and 2, GET and POST handlers can share the same path '/items' but respond differently based on the HTTP method.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which handler is called when the client sends a POST request to /items?
AgetAll()
Bupdate()
Ccreate()
Dremove()
💡 Hint
Check the row where HTTP Request is 'POST /items' in the execution table.
At which step does the route matching fail and no handler is called?
AStep 5
BStep 4
CStep 3
DStep 2
💡 Hint
Look for the step where 'Route Matched' is 'No match' and 'Handler Called' is 'None'.
If the PUT handler did not include ':id' in its route, what would happen when calling PUT /items/5?
AThe update() handler would still be called correctly.
BThe route would not match and a 404 would be returned.
CThe GET handler would be called instead.
DThe POST handler would be called instead.
💡 Hint
Check how route parameters like ':id' affect route matching in steps 3 and 4.
Concept Snapshot
NestJS route handlers respond to HTTP methods (GET, POST, PUT, DELETE).
Use decorators @Get(), @Post(), @Put(), @Delete() on controller methods.
Routes can include parameters like ':id' to capture URL parts.
NestJS matches request method and path to call the correct handler.
If no match, NestJS returns 404 automatically.
Full Transcript
In NestJS, route handlers are methods inside controllers decorated with HTTP method decorators like @Get, @Post, @Put, and @Delete. When a client sends an HTTP request, NestJS matches the request's path and method to the appropriate handler. For example, a GET request to '/items' calls the getAll() method decorated with @Get(). Routes can include parameters such as ':id' to capture dynamic parts of the URL, used in update() and remove() methods for PUT and DELETE requests. If no route matches, NestJS returns a 404 Not Found response. This flow ensures that each HTTP method and path combination triggers the correct code to handle the request and send back a response.