DEV Community

Cover image for Intro to Nest.js 🪺
Omher
Omher

Posted on

Intro to Nest.js 🪺

Nest.js is a NodeJS framework for building scalable server-side applications with TypeScript, it provides a suite of tools that leverage either fastify or express 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), similar to frameworks like Laravel or ruby on rails, 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>
Enter fullscreen mode Exit fullscreen mode

Nest.js CLI

🧠 Controller

Controller

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

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

Result

CLI Result

💉 Provider

Provider

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

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

🗄️ 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

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

CLI modules

Thanks for reading

Resources

Latest comments (0)