CRUD stands for Create, Read, Update, and Delete. These are the four basic operations to manage data in applications.
Click to reveal answer
beginner
In NestJS, which decorator is commonly used to define a method that handles HTTP POST requests for creating data?
The @Post() decorator is used in NestJS controllers to define a method that handles HTTP POST requests, typically for creating new data.
Click to reveal answer
intermediate
How does NestJS help organize CRUD operations in an application?
NestJS organizes CRUD operations using controllers for routing HTTP requests and services for business logic, keeping code clean and modular.
Click to reveal answer
beginner
What HTTP method and NestJS decorator pair is used to update existing data?
The HTTP PUT or PATCH method is used to update data. In NestJS, @Put() or @Patch() decorators define methods handling these requests.
Click to reveal answer
intermediate
Why is it important to separate controller and service logic in NestJS when implementing CRUD?
Separating controller and service logic improves code clarity, makes testing easier, and allows reusing business logic across different parts of the app.
Click to reveal answer
Which NestJS decorator is used to handle HTTP GET requests for reading data?
A@Post()
B@Delete()
C@Put()
D@Get()
✗ Incorrect
The @Get() decorator is used to define methods that handle HTTP GET requests, which are typically used to read or retrieve data.
What HTTP method is typically used to delete data in a RESTful API?
APOST
BGET
CDELETE
DPUT
✗ Incorrect
The DELETE HTTP method is used to remove data from the server.
In NestJS, where should the main business logic for CRUD operations be placed?
AIn the service
BIn the controller
CIn the module
DIn the main.ts file
✗ Incorrect
Business logic should be placed in services to keep controllers focused on handling requests and responses.
Which HTTP method and NestJS decorator pair is used to create new data?
AGET and @Get()
BPOST and @Post()
CPUT and @Put()
DDELETE and @Delete()
✗ Incorrect
POST requests, handled by @Post() in NestJS, are used to create new data.
What is the main purpose of the @Delete() decorator in NestJS?
ATo delete data
BTo create data
CTo update data
DTo read data
✗ Incorrect
The @Delete() decorator defines a method that handles HTTP DELETE requests to remove data.
Explain how CRUD operations are structured in a NestJS application.
Think about where requests are handled and where the data logic lives.
You got /4 concepts.
Describe the role of HTTP methods in CRUD operations and how NestJS maps them with decorators.
Match each CRUD action with its HTTP method and NestJS decorator.
You got /4 concepts.
Practice
(1/5)
1. In NestJS, which decorator is used in a controller method to handle HTTP POST requests for creating data?
easy
A. @Post
B. @Get
C. @Put
D. @Delete
Solution
Step 1: Understand HTTP methods and their NestJS decorators
HTTP POST is used to create new data. NestJS uses @Post decorator for this.
Step 2: Match the decorator to the HTTP method
@Get is for reading, @Put for updating, and @Delete for deleting data, so they don't fit creating data.
Final Answer:
@Post -> Option A
Quick Check:
POST = @Post [OK]
Hint: POST requests use @Post decorator in NestJS controllers [OK]
Common Mistakes:
Confusing @Get with @Post
Using @Put for creating data
Using @Delete for creation
2. Which of the following is the correct way to extract a route parameter named id in a NestJS controller method?
easy
A. async getItem(@Body('id') id: string) {}
B. async getItem(@Query('id') id: string) {}
C. async getItem(@Param('id') id: string) {}
D. async getItem(@Headers('id') id: string) {}
Solution
Step 1: Identify the decorator for route parameters
@Param is used to extract parameters from the URL path, such as /items/:id.
Step 2: Check other decorators and their uses
@Body extracts data from the request body, @Query from query strings, and @Headers from HTTP headers, so they don't get route params.
Final Answer:
async getItem(@Param('id') id: string) {} -> Option C
Quick Check:
Route param = @Param [OK]
Hint: Use @Param to get URL parameters in NestJS [OK]
Common Mistakes:
Using @Body to get URL params
Using @Query for route params
Confusing @Headers with @Param
3. Given this NestJS controller method, what will be the response if a client sends a DELETE request to /items/5 and the service deletes the item successfully?
D. Missing @Param decorator to get the id from URL
Solution
Step 1: Check how the id is obtained
The method expects id from updateDto.id, but the route has ':id' parameter which is not extracted.
Step 2: Identify missing @Param decorator
To get id from URL, @Param('id') id: string should be added as a parameter to the method.
Final Answer:
Missing @Param decorator to get the id from URL -> Option D
Quick Check:
URL param needs @Param [OK]
Hint: Use @Param to get URL id in update methods [OK]
Common Mistakes:
Assuming id comes from body without @Param
Confusing @Body with @Query
Ignoring route parameters
5. You want to implement a NestJS controller method to update an item by its id from the URL and new data from the request body. Which of the following method signatures correctly follows NestJS CRUD patterns?
D. async update(@Param() params: any) { return this.itemsService.update(params.id, params); }
Solution
Step 1: Identify correct decorators for id and body
Use @Param('id') to get id from URL and @Body() to get update data object.
Step 2: Check method parameters and service call
async update(@Param('id') id: string, @Body() updateDto: UpdateItemDto) { return this.itemsService.update(id, updateDto); } correctly separates id and updateDto and calls service with both.