Performance: Route parameters
Route parameters affect server-side routing speed and how quickly the server can match and process incoming requests.
import { Controller, Get, Param } from '@nestjs/common'; @Controller('users') export class UserController { @Get(':id') getUser(@Param('id') id: string) { return `User ID is ${id}`; } }
import { Controller, Get, Req } from '@nestjs/common'; @Controller('users') export class UserController { @Get('*') getUser(@Req() req) { const userId = req.url.split('/')[2]; // process userId manually return `User ID is ${userId}`; } }
| Pattern | CPU Usage | Routing Speed | Code Complexity | Verdict |
|---|---|---|---|---|
| Manual URL parsing | High (extra string ops) | Slower (extra processing) | High (manual code) | [X] Bad |
| NestJS @Param decorator | Low (optimized parsing) | Faster (native routing) | Low (clean code) | [OK] Good |
: symbol represent in a NestJS route path like @Get('users/:id')?: before id in @Get('users/:id') means id is a variable part of the URL.id values from the URL, making the route flexible.id as a dynamic route parameter to capture from the URL. -> Option DuserId inside a NestJS controller method?@Param() decorator is used to extract route parameters from the URL.@Param('userId') correctly accesses the userId parameter from the route.@Get('products/:id')
getProduct(@Param('id') id: string) {
return `Product ID is ${id}`;
}/products/42?@Param('id') decorator extracts the value 42 from the URL /products/42.id interpolated: "Product ID is 42".@Get('orders/:orderId')
getOrder(@Param() orderId: string) {
return `Order: ${orderId}`;
}@Param() decorator without an argument returns an object of all params, not a single string.orderId as a string, but @Param() without argument returns an object, causing a type mismatch.category and itemId. Which of the following route paths and method signatures correctly captures both parameters?'shop/:category/:itemId' correctly defines two dynamic route parameters.@Param('category') and @Param('itemId') extracts each parameter individually as strings.Pass the test and this pattern moves unknown