Route parameters are dynamic parts of a URL that can change. They let you capture values from the URL to use in your code, like an ID or a name.
Click to reveal answer
beginner
How do you define a route parameter in a NestJS controller?
You add a colon before the parameter name in the route path, like @Get(':id'). This tells NestJS to expect a dynamic value in that spot.
Click to reveal answer
beginner
How do you access route parameters inside a NestJS controller method?
Use the @Param() decorator to get the parameter value. For example, @Param('id') gets the value of the 'id' parameter from the URL.
Click to reveal answer
intermediate
What happens if you define multiple route parameters in NestJS?
You can define many parameters like @Get(':userId/orders/:orderId'). Each parameter can be accessed separately using @Param('userId') and @Param('orderId').
Click to reveal answer
beginner
Why are route parameters useful in web applications?
They let your app respond to different URLs with different data. For example, showing details for a specific user or product by using their ID in the URL.
Click to reveal answer
How do you declare a route parameter named 'id' in a NestJS controller?
A@Get(':id')
B@Get('id')
C@Get('{id}')
D@Get('/id')
✗ Incorrect
Route parameters use a colon before the name, so ':id' is correct.
Which decorator do you use to access a route parameter inside a NestJS method?
A@Param()
B@Headers()
C@Query()
D@Body()
✗ Incorrect
@Param() extracts route parameters from the URL.
What will @Param('userId') return if the route is '/users/42' and the route is defined as @Get('users/:userId')?
A:userId
Busers
C42
Dundefined
✗ Incorrect
The value '42' is captured as the userId parameter.
Can you have more than one route parameter in a single route in NestJS?
ANo, only one parameter is allowed
BYes, multiple parameters separated by slashes
CYes, but only if they have the same name
DNo, parameters must be in the query string
✗ Incorrect
You can define multiple parameters like ':userId/orders/:orderId'.
What is the main benefit of using route parameters?
ATo style the URL
BTo secure the route
CTo cache the response
DTo capture dynamic values from the URL for processing
✗ Incorrect
Route parameters let you get dynamic values from the URL to use in your app.
Explain how to define and access a route parameter in a NestJS controller.
Think about how URLs can have parts that change, like user IDs.
You got /3 concepts.
Describe a real-life example where route parameters are useful in a web app.
Imagine clicking on a link to see a specific item or profile.
You got /3 concepts.
Practice
(1/5)
1. What does the : symbol represent in a NestJS route path like @Get('users/:id')?
easy
A. It is used to comment out the route path.
B. It defines a query parameter to be passed in the URL.
C. It indicates a static part of the URL path.
D. It marks id as a dynamic route parameter to capture from the URL.
Solution
Step 1: Understand route path syntax
The colon : before id in @Get('users/:id') means id is a variable part of the URL.
Step 2: Identify the role of dynamic parameters
This dynamic parameter allows the route to accept different id values from the URL, making the route flexible.
Final Answer:
It marks id as a dynamic route parameter to capture from the URL. -> Option D
Quick Check:
Colon means dynamic parameter = A [OK]
Hint: Colon before name means dynamic URL part [OK]
Common Mistakes:
Thinking colon means query parameter
Confusing static and dynamic parts
Assuming colon is for comments
2. Which of the following is the correct way to access a route parameter named userId inside a NestJS controller method?
easy
A. @Body('userId') userId: string
B. @Query('userId') userId: string
C. @Param('userId') userId: string
D. @Headers('userId') userId: string
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 the userId parameter from the route.
Final Answer:
@Param('userId') userId: string -> Option C
Quick Check:
@Param for route params = C [OK]
Hint: Use @Param('name') to get route parameters [OK]
Common Mistakes:
Using @Query instead of @Param for route params
Trying to get route params from @Body or @Headers
Forgetting to specify parameter name in @Param
3. Given this NestJS controller method:
@Get('products/:id')
getProduct(@Param('id') id: string) {
return `Product ID is ${id}`;
}
What will be the response when a client requests /products/42?
medium
A. "Product ID is 42"
B. "Product ID is id"
C. "Product ID is undefined"
D. 404 Not Found error
Solution
Step 1: Understand route parameter extraction
The @Param('id') decorator extracts the value 42 from the URL /products/42.
Step 2: Check returned string
The method returns the string with the extracted id interpolated: "Product ID is 42".
Final Answer:
"Product ID is 42" -> Option A
Quick Check:
Route param 'id' = 42 in output [OK]
Hint: Route param value replaces :id in URL [OK]
Common Mistakes:
Expecting literal 'id' instead of value
Not using @Param to get the value
Assuming undefined if param missing
4. What is wrong with this NestJS controller method?
A. Missing parameter name inside @Param() decorator.
B. Route path should not have colon before orderId.
C. Method should use @Query instead of @Param.
D. Return statement syntax is invalid.
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 expects orderId as a string, but @Param() without argument returns an object, causing a type mismatch.
Final Answer:
Missing parameter name inside @Param() decorator. -> Option A
Quick Check:
@Param needs name for single param = B [OK]
Hint: Use @Param('name') for single param, else get object [OK]
Common Mistakes:
Omitting parameter name in @Param() when expecting string
Confusing @Param with @Query
Thinking colon is wrong in route path
5. You want to create a NestJS route that accepts two parameters: category and itemId. Which of the following route paths and method signatures correctly captures both parameters?
hard
A. @Get('shop/:category/:itemId') getItem(@Body('category') category: string, @Body('itemId') itemId: string)
B. @Get('shop/:category/:itemId') getItem(@Param('category') category: string, @Param('itemId') itemId: string)
C. @Get('shop/:category/:itemId') getItem(@Query('category') category: string, @Query('itemId') itemId: string)
D. @Get('shop/:category/:itemId') getItem(@Param() params: string)
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 B
Quick Check:
Use @Param('name') for each route param = D [OK]
Hint: Use @Param('name') for each route parameter [OK]
Common Mistakes:
Using @Param() with string instead of object
Using @Query or @Body instead of @Param for route params
Not matching parameter names exactly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.