Intro to Nest.js 🪺
Tags: nestjs, node, javascript, microservices
Published: 2023-02-26 18:04:58

`Nest.js` is a `NodeJS` framework for building scalable server-side applications with TypeScript, it provides a suite of tools that leverage either [fastify](https://www.fastify.io/) or [express](https://expressjs.com/) to facilitate rapid development and predictable readable code.

It supports `REST` and `GraphQL` APIs out of the box, or you might use it to build a Full Stack application using the model view controller pattern, aka [MVC](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller)), similar to frameworks like [Laravel](https://laravel.com/) or [ruby on rails](https://rubyonrails.org/), and it contains a ton of built-in modules to work with databases, handle security, implement streaming and anything else you can imagine doing in a
server side application.

`Nest.js` has it's own very powerful command line tool and you can scaffold out a new project with the nest new command that provides a code base pre-configured with `Jest` for testing and set up with TypeScript to help us write more readable and reliable code. 

To start just use the cli command: 

```
nest new <Project Name>
```

![Nest.js CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/epv439fc0bhzn34q3d0a.png)


## 🧠 Controller

![Controller](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jcc8pt6743pwuox45lq6.png)

In the source directory you'll notice a controller, which is a fundamental building block of the framework, it's responsible for handling incoming HTTP requests and returning responses back to the client. 

In order to implement a controller simply add the controller decorator to  class, then inside the class you can implement methods and decorate them with HTTP verbs like: `GET`, `POST`, `PATCH`, `PUT` and more.

By default this will create an HTTPS endpoint on the root URL , but you can pass a string to the decorator to change the route or implement dynamic route parameters. In addition `Nest.js` provides other decorators to control things like the status code and headers and more.

In addition in the method itself parameter decorators can be used to access the request parameters or body and finally the return value from the method is the response body that gets sent back down to the client.


![Nest.js Controller](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhafty60sjy3xezq9wph.png)

What's awesome about `Nest.js` is that you can use the `cli` to automatically generate more controllers to keep your code organized as it grows in complexity.

![CLI generate controller](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hfoz54el87uy4evo7jmq.png)

### Result

![CLI Result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/484r0sk9yo5kfsh6clmf.png)

## 💉 Provider

![Provider](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kbev7gndj77htd2akwx7.png)

A provider is a class that contains shared logic throughout the entire application and can be injected as a dependency where needed any class with the injectable decorator, can be injected in the constructor of another class, for example a provider can be implemented as a guard to handle role-based user authentication or it might be implemented as a pipe to efficiently validate and transform values in a controller.

![Using Providers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzq95xyfsvcr36v61ain.png)

Similar to controller you can use the `cli` to automatically generate more providers to keep your code organized as it grows in complexity.
 
![CLI Providers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3bo65qqz5jy645zcq2km.png)

## 🗄️ Module

Each application has at least one module, a root module. The root module is the starting point Nest uses to build the application graph.
The module decorator allows code to be organized into
smaller chunks where it can be lazy loaded to run faster in serverless environments and more

![Nest.js modules](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0m8i8orvf13i35fhof9j.png)

Again, like controllers and providers you can use the CLI to create a module:

![CLI modules](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dqo53kzol7nxt5j137t2.png)

 Thanks for reading

#### Resources

- [Repo](https://github.com/OmerHerera/omher-nestjs) in Github used in this post 
- Nest.js [documentation](https://docs.nestjs.com/)