Discover how NestJS route parameters save you from messy URL parsing headaches!
Why Route parameters in NestJS? - Purpose & Use Cases
Imagine building a web app where users can view profiles by typing URLs like /users/123. You try to read the user ID from the URL manually by parsing strings in every request handler.
Manually extracting parts of the URL is slow, repetitive, and easy to mess up. You might forget to handle missing or invalid IDs, leading to bugs and confusing errors for users.
Route parameters let NestJS automatically grab parts of the URL for you. You just declare the parameter name, and NestJS passes the value directly to your function, clean and ready to use.
const userId = req.url.split('/')[2]; // fragile and unclear
@Get('users/:id') getUser(@Param('id') id: string) { /* use id directly */ }
This makes your code cleaner, safer, and focused on what matters: handling the data, not parsing URLs.
When a user visits /products/456, your app instantly knows to fetch product 456 without extra string work.
Manual URL parsing is error-prone and repetitive.
Route parameters let NestJS extract URL parts automatically.
This leads to cleaner, safer, and easier-to-read code.
Practice
: symbol represent in a NestJS route path like @Get('users/:id')?Solution
Step 1: Understand route path syntax
The colon:beforeidin@Get('users/:id')meansidis a variable part of the URL.Step 2: Identify the role of dynamic parameters
This dynamic parameter allows the route to accept differentidvalues from the URL, making the route flexible.Final Answer:
It marksidas a dynamic route parameter to capture from the URL. -> Option DQuick Check:
Colon means dynamic parameter = A [OK]
- Thinking colon means query parameter
- Confusing static and dynamic parts
- Assuming colon is for comments
userId inside a NestJS controller method?Solution
Step 1: Identify decorator for route parameters
In NestJS,@Param()decorator is used to extract route parameters from the URL.Step 2: Match parameter name
Using@Param('userId')correctly accesses theuserIdparameter from the route.Final Answer:
@Param('userId') userId: string -> Option CQuick Check:
@Param for route params = C [OK]
- Using @Query instead of @Param for route params
- Trying to get route params from @Body or @Headers
- Forgetting to specify parameter name in @Param
@Get('products/:id')
getProduct(@Param('id') id: string) {
return `Product ID is ${id}`;
}What will be the response when a client requests
/products/42?Solution
Step 1: Understand route parameter extraction
The@Param('id')decorator extracts the value42from the URL/products/42.Step 2: Check returned string
The method returns the string with the extractedidinterpolated: "Product ID is 42".Final Answer:
"Product ID is 42" -> Option AQuick Check:
Route param 'id' = 42 in output [OK]
- Expecting literal 'id' instead of value
- Not using @Param to get the value
- Assuming undefined if param missing
@Get('orders/:orderId')
getOrder(@Param() orderId: string) {
return `Order: ${orderId}`;
}Solution
Step 1: Check @Param usage
The@Param()decorator without an argument returns an object of all params, not a single string.Step 2: Identify mismatch in parameter type
The method expectsorderIdas a string, but@Param()without argument returns an object, causing a type mismatch.Final Answer:
Missing parameter name inside @Param() decorator. -> Option AQuick Check:
@Param needs name for single param = B [OK]
- Omitting parameter name in @Param() when expecting string
- Confusing @Param with @Query
- Thinking colon is wrong in route path
category and itemId. Which of the following route paths and method signatures correctly captures both parameters?Solution
Step 1: Verify route path with two parameters
The path'shop/:category/:itemId'correctly defines two dynamic route parameters.Step 2: Check method parameters for correct decorators
Using@Param('category')and@Param('itemId')extracts each parameter individually as strings.Final Answer:
@Get('shop/:category/:itemId') with getItem(@Param('category') category: string, @Param('itemId') itemId: string) -> Option BQuick Check:
Use @Param('name') for each route param = D [OK]
- Using @Param() with string instead of object
- Using @Query or @Body instead of @Param for route params
- Not matching parameter names exactly
Ready to prove it?
Pass the test and this pattern moves unknown
