Controllers Flashcards

1
Q

What is the primary responsibility of controllers in an application?

A

Handling incoming requests and sending responses back to the client.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What determines which controller will handle each request?

A

The routing mechanism.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can a controller handle multiple routes?

A

Yes, a controller often has multiple routes, and each route can perform a different action.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is used to create a basic controller?

A

Classes and decorators.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What role do decorators play in creating a controller?

A

They link classes with the necessary metadata, allowing Nest to create a routing map.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can you quickly create a CRUD controller with built-in validation?

A

Use the CLI’s CRUD generator: nest g resource [name].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What decorator is required to define a basic controller in NestJS?

A

@Controller()

The @Controller() decorator is essential for creating a controller in NestJS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of specifying an optional route path prefix in the @Controller() decorator?

A

To group related routes together and reduce repetitive code

This helps maintain cleaner and more organized code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What command is used to create a controller using the NestJS CLI?

A

$ nest g controller [name]

Replace [name] with the desired controller name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the @Get() decorator indicate in a NestJS controller?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is the route path for a handler determined in NestJS?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the default status code returned by NestJS for a successful response?

A

200

For POST requests, the default status code is 201.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happens when a request handler returns a JavaScript object or array?

A

It will automatically be serialized to JSON

This is part of the standard response handling in NestJS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What decorator allows the use of the library-specific response object in NestJS?

A

@Res()

This decorator injects the response object into the method handler.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What must be set to use both standard and library-specific response handling in the same route?

A

Set the passthrough option to true in the @Res({ passthrough: true }) decorator

This allows partial use of both response handling methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

True or False: The method name in a NestJS controller must have specific significance.

A

False

The method name is arbitrary and does not affect the routing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the purpose of the request object in Nest?

A

It provides access to the client’s request details.

18
Q

How can you access the request object in a Nest handler?

A

By using the @Req() decorator in the handler’s signature.

19
Q

Which HTTP framework does Nest use by default for the request object?

20
Q

What is the function of the @Get() decorator?

A

It defines a route handler for GET requests.

21
Q

What package needs to be installed to take advantage of express typings?

A

@types/express

22
Q

What properties does the request object contain?

A

Query string, parameters, HTTP headers, and body.

23
Q

True or False: You always need to manually access properties of the request object.

24
Q

What are some dedicated decorators available in Nest for accessing request properties?

A
  • @Body()
  • @Query()
  • @Param()
  • @Headers()
  • @Ip()
25
Fill in the blank: The @Response() decorator exposes the underlying ________ object interface.
native platform response
26
What happens when you inject @Res() in a method handler?
You put Nest into Library-specific mode for that handler.
27
What must you do if you use @Res() or @Response() in a handler?
Issue a response using the response object.
28
What is the effect of not issuing a response when using @Res()?
The HTTP server will hang.
29
What is the purpose of the @Session() decorator?
To access session data in the request.
30
What does the @Param(key?: string) decorator correspond to?
req.params / req.params[key]
31
What does @Body(key?: string) correspond to?
req.body / req.body[key]
32
What is the use of the @Next() decorator?
To access the next middleware function.
33
What does the @Query(key?: string) decorator correspond to?
req.query / req.query[key]
34
What does the @Headers(name?: string) decorator allow you to access?
req.headers / req.headers[name]
35
What is the purpose of the @Ip() decorator?
To access the IP address of the request.
36
What does the @HostParam() decorator provide access to?
req.hosts
37
What is the purpose of the POST handler in the CatsController?
To create new records, specifically to add a new cat ## Footnote The POST method is used to submit data to be processed to a specified resource.
38
What does the @Get() decorator do in the CatsController?
It defines an endpoint that returns all cats ## Footnote The GET method is typically used to retrieve data from a server.
39
List all the standard HTTP methods provided by Nest decorators.
* @Get() * @Post() * @Put() * @Delete() * @Patch() * @Options() * @Head() ## Footnote These decorators simplify the creation of RESTful APIs.
40
What does the @All() decorator do?
It defines an endpoint that handles all HTTP methods ## Footnote Useful for endpoints that need to accept multiple types of requests.
41
Fill in the blank: The action that adds a new cat is defined in the _______ method.
POST
42
True or False: The CatsController can handle both creating and retrieving cat records.
True