Controllers Flashcards
What is the primary responsibility of controllers in an application?
Handling incoming requests and sending responses back to the client.
What determines which controller will handle each request?
The routing mechanism.
Can a controller handle multiple routes?
Yes, a controller often has multiple routes, and each route can perform a different action.
What is used to create a basic controller?
Classes and decorators.
What role do decorators play in creating a controller?
They link classes with the necessary metadata, allowing Nest to create a routing map.
How can you quickly create a CRUD controller with built-in validation?
Use the CLI’s CRUD generator: nest g resource [name].
What decorator is required to define a basic controller in NestJS?
@Controller()
The @Controller() decorator is essential for creating a controller in NestJS.
What is the purpose of specifying an optional route path prefix in the @Controller() decorator?
To group related routes together and reduce repetitive code
This helps maintain cleaner and more organized code.
What command is used to create a controller using the NestJS CLI?
$ nest g controller [name]
Replace [name] with the desired controller name.
What does the @Get() decorator indicate in a NestJS controller?
It tells Nest to create a handler for a specific endpoint for HTTP GET requests
This decorator is used to define routes that respond to GET requests.
How is the route path for a handler determined in NestJS?
By combining the optional prefix declared for the controller with any path specified in the method’s decorator
Example: If the controller prefix is ‘cats’ and the method decorator is @Get(‘breed’), the route will be GET /cats/breed.
What is the default status code returned by NestJS for a successful response?
200
For POST requests, the default status code is 201.
What happens when a request handler returns a JavaScript object or array?
It will automatically be serialized to JSON
This is part of the standard response handling in NestJS.
What decorator allows the use of the library-specific response object in NestJS?
@Res()
This decorator injects the response object into the method handler.
What must be set to use both standard and library-specific response handling in the same route?
Set the passthrough option to true in the @Res({ passthrough: true }) decorator
This allows partial use of both response handling methods.
True or False: The method name in a NestJS controller must have specific significance.
False
The method name is arbitrary and does not affect the routing.
What is the purpose of the request object in Nest?
It provides access to the client’s request details.
How can you access the request object in a Nest handler?
By using the @Req() decorator in the handler’s signature.
Which HTTP framework does Nest use by default for the request object?
Express
What is the function of the @Get() decorator?
It defines a route handler for GET requests.
What package needs to be installed to take advantage of express typings?
@types/express
What properties does the request object contain?
Query string, parameters, HTTP headers, and body.
True or False: You always need to manually access properties of the request object.
False
What are some dedicated decorators available in Nest for accessing request properties?
- @Body()
- @Query()
- @Param()
- @Headers()
- @Ip()