0
0
NestJSframework~20 mins

Route parameters in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Route Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing a route with parameters?
Consider this NestJS controller method:
  @Get('user/:id')
  getUser(@Param('id') id: string) {
    return `User ID is ${id}`;
  }

What will be the response body when a client requests /user/42?
A"User ID is user/42"
B"User ID is :id"
C"User ID is 42"
D"User ID is undefined"
Attempts:
2 left
💡 Hint
Route parameters are extracted from the URL and passed to the method.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a route with two parameters?
You want to create a GET route in NestJS that matches URLs like /order/123/item/456 and extracts both orderId and itemId as parameters.
Which method signature is correct?
A
@Get('order/:orderId/item/:itemId')
getOrderItem(@Param('orderId') orderId: string, itemId: string) { return {orderId, itemId}; }
B
@Get('order/:orderId/item/:itemId')
getOrderItem(@Param() params: {orderId: string, itemId: string}) { return params; }
C
@Get('order/:orderId/item/:itemId')
getOrderItem(@Param('orderId', 'itemId') ids: string[]) { return ids; }
D
@Get('order/:orderId/item/:itemId')
getOrderItem(@Param('orderId') orderId: string, @Param('itemId') itemId: string) { return {orderId, itemId}; }
Attempts:
2 left
💡 Hint
Each parameter must be decorated separately or use @Param() to get all.
🔧 Debug
advanced
2:00remaining
Why does this route parameter extraction fail?
Given this controller method:
@Get('product/:id')
getProduct(@Param() id: string) {
  return `Product ID: ${id}`;
}

What error or issue will occur when calling /product/10?
ATypeError because id is an object, not a string
BReturns 'Product ID: 10' correctly
CSyntaxError due to missing colon in route
DRuntime error because @Param() is missing argument
Attempts:
2 left
💡 Hint
Check what @Param() returns when no argument is given.
🧠 Conceptual
advanced
2:00remaining
How does NestJS handle optional route parameters?
You want to define a route where the parameter category is optional, matching both /products and /products/electronics.
Which route path correctly supports this?
A@Get('products/(category)')
B@Get('products/:category?')
C@Get('products/:category')
D@Get('products/:category*')
Attempts:
2 left
💡 Hint
Optional parameters use a question mark in the path.
state_output
expert
3:00remaining
What is the output of this controller with nested route parameters?
Given this NestJS controller snippet:
@Controller('users')
export class UserController {
  @Get(':userId/posts/:postId')
  getPost(@Param() params: { userId: string; postId: string }) {
    return `User ${params.userId} Post ${params.postId}`;
  }
}

What is the response body when a client requests /users/7/posts/15?
A"User 7 Post 15"
B"User :userId Post :postId"
C"User 7 Post :postId"
D"User undefined Post undefined"
Attempts:
2 left
💡 Hint
The @Param() decorator without argument returns an object with all parameters.