0
0
NestJSframework~10 mins

Route parameters in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a route parameter named 'id' in a NestJS controller method.

NestJS
  @Get(':[1]')
  getItem(@Param('id') id: string) {
    return `Item ID is ${id}`;
  }
Drag options to blanks, or click blank then click option'
Aid
Bparam
Citem
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different name in the route path and in @Param decorator.
Forgetting the colon ':' before the parameter name.
2fill in blank
medium

Complete the code to extract the 'userId' route parameter in the controller method.

NestJS
  @Get('user/:userId')
  getUser(@Param('[1]') userId: string) {
    return `User ID is ${userId}`;
  }
Drag options to blanks, or click blank then click option'
Aparam
Bid
CuserId
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different string inside @Param than the route parameter name.
Forgetting to include the parameter name in the route path.
3fill in blank
hard

Fix the error in the code to correctly define and use a route parameter named 'postId'.

NestJS
  @Get(':[1]')
  getPost(@Param('postId') postId: string) {
    return `Post ID is ${postId}`;
  }
Drag options to blanks, or click blank then click option'
Apost
Bpid
Cid
DpostId
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatch between route parameter name and @Param string.
Using a different variable name in the method parameter than in the decorator.
4fill in blank
hard

Fill both blanks to create a route with two parameters 'category' and 'itemId' and extract them in the method.

NestJS
  @Get(':[1]/:[2]')
  getItem(@Param('[1]') category: string, @Param('[2]') itemId: string) {
    return `Category: ${category}, Item ID: ${itemId}`;
  }
Drag options to blanks, or click blank then click option'
Acategory
Bitem
CitemId
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names in the route and @Param decorators.
Forgetting the colon ':' before route parameters.
5fill in blank
hard

Fill all three blanks to create a route with parameters 'user', 'postId', and 'commentId' and extract them in the method.

NestJS
  @Get(':[1]/:[2]/:[3]')
  getComment(
    @Param('[1]') user: string,
    @Param('[2]') postId: string,
    @Param('[3]') commentId: string
  ) {
    return `User: ${user}, Post: ${postId}, Comment: ${commentId}`;
  }
Drag options to blanks, or click blank then click option'
Auser
BpostId
CcommentId
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up parameter names between route and @Param.
Forgetting to add colons before parameters in the route.