docs.nestjs.com/controllers
12 Users
1 Comments
243 Highlights
2 Notes
Tags
Top Highlights
Controllers are responsible for handling incoming requests and returning responses to the client.
A controller's purpose is to receive specific requests for the application.
n order to create a basic controller, we use classes and decorators.
The route path for a handler is determined by concatenating the (optional) prefix declared for the controller, and any path specified in the method's decorator.
@Controller() decorator allows us to easily group a set of related routes,
the @Controller() decorator, which is required to define a basic controller.
Routing
route path
each controller has more than one route, and different routes can perform different actions.
specify an optional route path prefix of cats
Node.js doesn't follow the request/response Multi-Threaded Stateless Model in which every request is processed by a separate thread.
This method will return a 200 status code and the associated response, which in this case is just a string. Why does that happen? To explain, we'll first introduce the concept that Nest employs two different options for manipulating responses:
Request object# Handlers often need access to the client request details. Nest provides access to the request object of the underlying platform (Express by default). We can access the request object by instructing Nest to inject it by adding the @Req() decorator to the handler's signature.
@Post() create(): string { return 'This action adds a new cat'; }
@All() defines an endpoint that handles all of them
the response status code is always 200 by default, except for POST requests which are 201. We can easily change this behavior by adding the @HttpCode(...) decorator at a handler level.
To specify a custom response header, you can either use a @Header() decorator or a library-specific response object (and call res.header() directly).
To redirect a response to a specific URL, you can either use a @Redirect()
The default value of statusCode is 302 (Found)
Sometimes you may want to determine the HTTP status code or the redirect URL dynamically
Toughts & Comments
Shun Ishigaki
A controller's purpose is to receive specific requests for the application. The routing mechanism controls which controller receives which requests. Frequently, each controller has more than one route, and different routes can perform different actions. In order to create a basic controller, we use classes and decorators. Decorators associate classes with required metadata and enable Nest to create a routing map (tie requests to the corresponding controllers).
Glasp is a social web highlighter that people can highlight and organize quotes and thoughts from the web, and access other like-minded people’s learning.