Section 4,5:
Now, to begin learning other new things, we begin a simple “messages” application. We will simply retrieve and post data to a static JSON file.
So, this above chunk is a visualization of what the finished product will look like.
To begin, it is useful to make use of Nest.js CLI to make creating things much faster.
Once we have set up the request handlers, we can simply test using either POSTMAN, or a VSCode extension: REST client. Then, we make the file, something like, “requests.http”, and populate it with:
So, with these decorators, we can parse the URL to obtain what we want. This can be the query string, a specific parameter, or the body of the response, as shown above.
So, at this point, we have created a controller. Now, say we want to validate data that is being posted. We use a pip for this purpose.
ValidationPipe is the built-in Nest pipe to validate things. To create a validation pipe, we simply:
Now, we need to create a special set of rules for the validation pipe to follow.
We need only to to step 1, once, which is the code above. For every thing we want to validate, we will need to repeat steps 2,3,4.
In particular, for step 2, the class we make is called a Data Transfer Object (dto). This is what you’ve been seeing in the company back-end API repo. So, here is to code to make sure that the content of a POST request is a string:
The diagram above illustrates what is happening here. Recall that we needed to npm install class-transformer class-validator
. These are what power this process. First, the JSON is converted into an instance of the DTO class (in our case, the CreateMessageDto). Then, after we have a DTO object, the decorators run to run validation with class-validator. Lastly, it checks if there are errors.
So, let’s see how to implement the repository and the service. Code is simple, can be found in the messages folder.
Now, one of the most important concepts about Nest.js is dependency injection. While it is very easy to briefly go over the syntax and how to use it, it is imperative we understand what it is trying to accomplish.
To begin, we go over some important concepts.
Inversion of Control Principle
This principle states: Classes should not create instances of its dependencies on its own. Earlier, we said that this:
was a terrible. Instead, what is the best way of doing this is:
However, this principle also comes with its downsides.
With this, it is very lengthy. We now need 3 lines to create one controller as opposed to the one line for the original code. This may multiply by many times when we add more services and controllers. To solve this problem, we introduce dependency injection
Dependency Injection
To solve the above problem and maintain inversion of control, we use this.
So, now that we know how the internals of dependency injection work, let’s refactor out code to use it. To see this, visit the project folder.