herbert mühlex
herbert mühlex

Reputation: 291

handler path in Nest.js

I have this PATCH request: http://localhost:3000/tasks/566-344334-3321/status.

With the handler for that request:

@Patch('/:id/status')
updateTaskStatus() { // do stuff 
    return "got through";
}

I dont understand the mechanics behind the stem part and how the correct handler gets assigned to handle the request.

So from /566-344334-3321/status
The id part is /566-344334-3321 which can be any value.
But the stem ending /status needs to be exactly /status.
If not, a "error": "Not Found" occurs.

What is the underlying logic behind this behavior ?

Upvotes: 0

Views: 964

Answers (1)

Hugo Sohm
Hugo Sohm

Reputation: 3310

To get your id parameter, you need to use the @Param decorator to assign the :id in your request to a typescript number.

Here is how you'r supposed to write your updateTaskStatus function according to the NestJS documentation about Route Parameters

@Patch('/:id/status')
updateTaskStatus(@Param('id') id: number) { 
    return `Got ${id} through`;
}

If you question was to use the status as a dynamic value like id, you need to apply the same decorator to your parameter

@Patch('/:id/:status')
updateTaskStatus(@Param('id') id: number, @Param('status') status: string) { 
    return `Got ${id} and ${status} through`;
}

The NestJS documentation is very good and complete, don't hesitate to read the documentation about the controllers

Upvotes: 2

Related Questions