Bird
Raised Fist0
NestJSframework~20 mins

Route parameters in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 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.

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

  1. Step 1: Understand route path syntax

    The colon : before id in @Get('users/:id') means id is a variable part of the URL.
  2. 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.
  3. Final Answer:

    It marks id as a dynamic route parameter to capture from the URL. -> Option D
  4. 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

  1. Step 1: Identify decorator for route parameters

    In NestJS, @Param() decorator is used to extract route parameters from the URL.
  2. Step 2: Match parameter name

    Using @Param('userId') correctly accesses the userId parameter from the route.
  3. Final Answer:

    @Param('userId') userId: string -> Option C
  4. 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

  1. Step 1: Understand route parameter extraction

    The @Param('id') decorator extracts the value 42 from the URL /products/42.
  2. Step 2: Check returned string

    The method returns the string with the extracted id interpolated: "Product ID is 42".
  3. Final Answer:

    "Product ID is 42" -> Option A
  4. 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?
@Get('orders/:orderId')
getOrder(@Param() orderId: string) {
  return `Order: ${orderId}`;
}
medium
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

  1. Step 1: Check @Param usage

    The @Param() decorator without an argument returns an object of all params, not a single string.
  2. 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.
  3. Final Answer:

    Missing parameter name inside @Param() decorator. -> Option A
  4. 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

  1. Step 1: Verify route path with two parameters

    The path 'shop/:category/:itemId' correctly defines two dynamic route parameters.
  2. Step 2: Check method parameters for correct decorators

    Using @Param('category') and @Param('itemId') extracts each parameter individually as strings.
  3. Final Answer:

    @Get('shop/:category/:itemId') with getItem(@Param('category') category: string, @Param('itemId') itemId: string) -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard