What is the purpose of a GET route handler in NestJS?
A GET route handler is used to retrieve data from the server. It responds to client requests by sending back information without changing any data on the server.
Click to reveal answer
beginner
How do you define a POST route handler in NestJS?
You use the @Post() decorator above a method inside a controller. This method handles requests that send data to the server, usually to create new resources.
Click to reveal answer
intermediate
What is the difference between PUT and POST route handlers?
POST creates new resources, while PUT updates existing resources. PUT is idempotent, meaning calling it multiple times has the same effect as once, unlike POST.
Click to reveal answer
beginner
How do you handle DELETE requests in NestJS?
You use the @Delete() decorator on a controller method. This method deletes a resource on the server when the client sends a DELETE request.
Click to reveal answer
intermediate
Why is it important to use the correct HTTP method in route handlers?
Using the correct HTTP method helps the server and client understand the action being performed, keeps APIs clear, and supports proper caching and security.
Click to reveal answer
Which decorator is used to create a route handler for retrieving data in NestJS?
A@Get()
B@Post()
C@Put()
D@Delete()
✗ Incorrect
The @Get() decorator defines a route handler that responds to GET requests, which are used to retrieve data.
What HTTP method should you use to update an existing resource?
AGET
BPOST
CPUT
DDELETE
✗ Incorrect
PUT is used to update existing resources on the server.
Which route handler decorator would you use to delete a resource?
A@Get()
B@Post()
C@Put()
D@Delete()
✗ Incorrect
The @Delete() decorator handles DELETE requests to remove resources.
What is the main use of the POST route handler?
ARetrieve data
BCreate new data
CUpdate data
DDelete data
✗ Incorrect
POST is used to send data to the server to create new resources.
Which HTTP method is idempotent, meaning multiple identical requests have the same effect as one?
APUT
BGET
CPOST
DDELETE
✗ Incorrect
PUT is idempotent; calling it multiple times does not change the result beyond the initial update.
Explain how to create route handlers for GET, POST, PUT, and DELETE requests in NestJS.
Think about the HTTP methods and their purpose in REST APIs.
You got /5 concepts.
Why is it important to match the HTTP method with the correct route handler in NestJS?
Consider how HTTP methods describe the action intended.
You got /4 concepts.
Practice
(1/5)
1. Which decorator is used in NestJS to create a route handler that responds to HTTP GET requests?
easy
A. @Get
B. @Post
C. @Put
D. @Delete
Solution
Step 1: Understand HTTP methods and their decorators
In NestJS, each HTTP method has a specific decorator: @Get for GET, @Post for POST, @Put for PUT, and @Delete for DELETE.
Step 2: Match the decorator to the GET method
The decorator @Get is specifically designed to handle HTTP GET requests.
Final Answer:
@Get -> Option A
Quick Check:
GET method uses @Get decorator [OK]
Hint: GET requests use @Get decorator in NestJS [OK]
Common Mistakes:
Confusing @Post or @Put with @Get
Using @Delete for GET requests
Not using any decorator
2. Which of the following is the correct syntax to define a POST route handler in a NestJS controller?
easy
A. @Post('/create') createItem() {}
B. Post('/create') createItem() {}
C. @post('/create') createItem() {}
D. @Put('/create') createItem() {}
Solution
Step 1: Check the decorator syntax
Decorators in NestJS start with '@' and are case-sensitive. The correct decorator for POST is '@Post'.
Step 2: Verify the route path and method name
The route path '/create' is correctly passed as a string argument, and the method name is valid.
Final Answer:
@Post('/create') createItem() {} -> Option A
Quick Check:
Correct decorator syntax uses '@Post' with parentheses [OK]
Hint: Decorators start with '@' and are case-sensitive [OK]
Common Mistakes:
Missing '@' before decorator
Using lowercase '@post' instead of '@Post'
Using wrong decorator like '@Put' for POST
3. Given the following NestJS controller code, what will be the HTTP method and route path to access the handler?
A. The method name must be 'deleteItem' for @Delete decorator
B. The decorator @Delete cannot have a path argument
C. The controller path 'items' is invalid
D. The method should return a value, not just console.log
Solution
Step 1: Check the decorator and path usage
@Delete can have a path argument like '/remove', so this is valid.
Step 2: Check the method implementation
The method only logs to console but does not return any response. HTTP handlers should return a response to the client.
Final Answer:
The method should return a value, not just console.log -> Option D
Quick Check:
Route handlers must return a response [OK]
Hint: Route handlers must return a response, not just log [OK]
Common Mistakes:
Assuming console.log sends response
Thinking decorator cannot have path
Believing method name must match decorator
5. You want to create a NestJS controller with a route handler that updates a product by its ID using HTTP PUT. The handler should accept the ID as a route parameter and the updated data in the request body. Which code snippet correctly implements this?