Previously: In the last month, I started to make a sweepstakes based on the nest.js server, so I would like to share with you. Content: This article is annotated based on the examples from the official website, which only covers the introduction to the use and concept of Nest.js. Aim: Simply string together several common nest.js concepts for people who have never had contact with them. Related documents: English document-link, Chinese document-link ** ** Nest. js is the node.js framework I’ve worked with the most, although I’ve also used express.js and koa.js briefly… BUT I don’t want to go into too much about the differences and connections between them. Maybe I’ll share it in a separate article (if I need to do more research). This article will be introduced according to the following logic:

  • A simple request
  • A slightly more complex request
  • The concept of “doing” has to be mentioned
  • A truly complete set of requests

It is recommended to see the original text because there are some key identifiers (these are filtered out). The original address: www.yuque.com/u548790/tec…

An overview of the

When we use Nex.js, we don’t need to worry too much about what the underlying logic is. But if we want to use a lower-level framework, such as Express.js, with Nest.js, we can also do it declaratively.

Major sharing

Here are some of the things that you will encounter frequently during nest. Js development: Controller, Privider, Module, Middleware, Exception Filter, PIPE, Guard, Interceptor, and Decotator.

A simple request


Let’s start with the first simple request.(Please look carefully at the picture and its annotation)



The above is a note I wrote using an example from the official website. In Nex.js we can use a large number of decorators already wrapped, or we can wrap our own decorators. The main purpose of decorators is to make code clearer. They are syntactic sugar for specific classes.

When we define a set of controllers, the routing mechanism of Nest.js automatically helps us map. The corresponding request is straight up and down, very clear: POST: cats -> No other path -> request create method -> return the corresponding value (purple arrow on the left of the image above) GET: Cats -> no other path -> request findAll method -> return the corresponding value (yellow arrow on the left) if you want to GET information about the cat vivi, just at @get (‘vivi’). In order to make the function and code more explicit, we usually put the code of similar function/body together, this is cats related operations, routes together, all in the cats.controller.ts folder. To be specific, create a new kitten, delete a kitten, change the name of a kitten, query all kittens, all kitty-related requests we put in our cats.controller.ts file.

It is possible to get request parameters, set other paths for Cats/XXX, dynamic routing, and set other HTTP requests, all of which are basically implemented through the built-in decorator. Here is a complete simple example (just skim it).

A slightly more complex request


If we’re working on a project, it’s generally a microcomplex request, so it’s generally used this way. Instead of handing the request details to Conroller, we put him in a separate file Provider to relieve the pressure on the Controller.



Just tell it in the Controller that I need the Provider’s help, and then we can do “whatever” we can in the corresponding Provider based on the functionality, and we can reference the public part Provider to help us do things.

A Controller can correspond to multiple providers. Classify providers according to their functions and give them a good name.



The first step is to define it in the Controller to inform the Controller that the next processing is for a Provider. (left)

The second is to implement specific methods within the Provider. (right)



(There are other ways to declare a service, in which the XXDto is defined by TS request/corresponding data type, is under the category of TS.)



This is a complete, commonly used request-response pattern. (The response can also be customized as shown in the standard mode, or use a specific library such as express.js mode, of course, you can also customize the corresponding header, HTTP code, HTTP status, etc.)



The above reference to CatSService in Controller is an example of dependency injection, which is also an important concept in Nest.js.

The concept of “doing” has to be mentioned separately


We only defined a set of routes and its implementation method above, which is equivalent to building a windmill in a windless area. Without wind, it cannot move, so the concept of Module must be put forward separately.



In the Module, we can define the Controller and its corresponding Provider. We can also export the Provider and import the required public Provider of other modules to use. The premise is that all entries must be defined in the Module before they can be used normally.





(Modules can also be divided into public modules, common modules, and modules can also be defined in different forms. Above is the Cats Module defined by the decorator @module.)

A truly complete set of requests

The above is just a simple definition of the Module, and it has requested the Controller and Provider to complete its task. However, in practical application, we will encounter many unexpected situations:

  • There are multiple user roles in the background of the management system. Users can access only specified routes. How can I manage this situation?
  • What should I do if I encounter an error in the process?
  • Each company generally has a specific definition of the unified request return content, how to deal with?
  • How to deal with some public or proposed authentication interfaces?
  • What if the request has multiple validations?
  • . .

Nex.js has its own processing methods for the above content, but we have illustrated how we can completely manage and handle a request through Nex.js. (Assuming we have defined the content according to the official website)

Due to the Denver nuggets don’t support PlantUML syntax, recommend a click here to view: www.yuque.com/u548790/tec…



Ah!

The above!