Bird
Raised Fist0
NestJSframework~10 mins

Route parameters in NestJS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Route parameters
Client sends request to URL with parameter
↓
NestJS Router matches route pattern
↓
Extract parameter from URL
↓
Pass parameter to controller method
↓
Controller uses parameter to process request
↓
Send response back to client
This flow shows how NestJS takes a URL with parameters, extracts them, and passes them to the controller method to handle the request.
Execution Sample
NestJS
import { Controller, Get, Param } from '@nestjs/common';

@Controller('users')
export class UsersController {
  @Get(':id')
  getUser(@Param('id') id: string) {
    return `User ID is ${id}`;
  }
}
This code defines a route with a parameter 'id' and returns it in the response.
Execution Table
StepIncoming URLRoute Pattern MatchedExtracted Param 'id'Controller Method CalledResponse
1/users/42/users/:id42getUser('42')User ID is 42
2/users/abc/users/:idabcgetUser('abc')User ID is abc
3/users/No match--404 Not Found
💡 Execution stops when no route pattern matches the incoming URL or after sending the response.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
id-42abc-
Key Moments - 2 Insights
Why does the route '/users/' not match the pattern '/users/:id'?
Because the pattern expects a parameter after '/users/', but the URL ends there, so no 'id' is extracted (see execution_table row 3).
How does NestJS know which part of the URL is the parameter?
NestJS uses the ':id' in the route pattern to extract that part of the URL as the 'id' parameter (see execution_table rows 1 and 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the extracted 'id' when the URL is '/users/42'?
A42
Busers
Cid
DNot extracted
💡 Hint
Check the 'Extracted Param 'id'' column in execution_table row 1.
At which step does the route pattern fail to match the incoming URL?
AStep 1
BStep 2
CStep 3
DNone
💡 Hint
Look at the 'Route Pattern Matched' column in execution_table row 3.
If the URL was '/users/123/profile', would the current route pattern '/users/:id' match?
AYes, 'id' is '123/profile'
BNo, because extra path segments exist
CYes, 'id' is '123'
DNo, because 'profile' is missing
💡 Hint
Route parameters match one segment; extra segments cause no match.
Concept Snapshot
Route parameters in NestJS let you capture parts of the URL.
Use ':paramName' in route paths.
Access with @Param('paramName') in controller methods.
Example: '/users/:id' captures 'id' from URL.
If URL doesn't match pattern, request fails (404).
Useful for dynamic URLs like user profiles.
Full Transcript
In NestJS, route parameters allow the server to capture dynamic parts of the URL. When a client sends a request like '/users/42', NestJS matches this to a route pattern like '/users/:id'. It extracts '42' as the parameter 'id' and passes it to the controller method decorated with @Param('id'). The controller can then use this value to process the request and send a response. If the URL does not match the pattern, such as '/users/' without an id, NestJS returns a 404 error. This mechanism helps build flexible routes that respond to different inputs dynamically.

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