DEV Community

loading...
Cover image for Getting Started With NestJS
This is Learning

Getting Started With NestJS

Santosh Yadav
GDE Angular, GitHub Star, Auth0 Ambassador ,Open source contributor, Co-Founder ThisisLearning
Originally published at Medium Updated on ・3 min read

This is the first part of the series of articles on the Node.js framework known as NestJS, in this article we will see why and how to use NestJS.

My Node.js Story

I have been using Express.js from 2017 and I love ExpressJS, it is well designed and we can plugin any JavaScript library and use it. I was a .Net developer for a long time and working with Angular now, and I do miss a few things while working with ExpressJS.

Angular ecosystem vs Node.js/Express.js ecosystem

  • CLI: Angular offers us CLI to easily get started with a new project, even in .Net I can create an App using CLI. Though there are many Generators which are available, a CLI with ExpressJS would have been a great addition. Clean Architecture: ExpressJS doesn't come with any clean architecture defined, and of course it's not the purpose of ExpressJS as you are free to define your own architecture, But for an enterprise Application I will prefer something which has a clean and a well-defined Architecture.
  • Code Sharing: For a big enterprise application we may need to share the code across multiple apps or even APIs. In other programming languages, it can be achieved using Libraries, In ExpressJS we can to create an npm module and make it available via artifactory.

NestJS to the Rescue

NestJS is a framework that is written on top of ExpressJS and it is written in Typescript. Let's see some advantages

  • Typescript Support: NestJS supports Typescript which makes me really comfortable as I have been using Typescript for a long time while working with Angular. You have the option to choose Javascript as well.
  • Code Sharing: NestJS supports creating Libraries and Applications using CLI, it becomes really easy to share the code and becomes a great choice for enterprise applications.
  • monorepo Support: Angular Supports monorepo starting version 6, NestJS comes with monorepo support.
  • Learning Path: Another thing which I liked about NestJS is if you are coming from .Net or Java background and have an idea about creating APIs, NestJS is easy to learn. Also if you are an Angular developer you will feel home, as it follows the same modular pattern.
  • Fastify Support: NestJS uses ExpressJS as the default framework, but it also has support for Fastify and can be easily configured.

Architecture

If I have to define the architecture of API created using NestJS this is how it looks like, We have a root module available which will be used to configure Database Providers, defining controller, adding middleware, adding pipe and guards and providing services.

We can also have a Module for each controller, we will see how to achieve that in upcoming blog posts. Once our module receives a request, it will be redirected to the respective controller which will handle the request, the service is optional, but we should try to use service to follow the Single Responsibility.

Installation

Now we have an idea about why we should use NestJS let's see how to use it.
Before we can start using NestJS we need to install NestJS CLI, run the below command to install CLI globally.

npm install -g @nestjs/cli
Enter fullscreen mode Exit fullscreen mode

You can also download the Starter Project from GitHub and use it.

Creating our First API

  • Once CLI is installed run the below command to create a new Application named shoppingAPI, we will use the same API for our upcoming articles.
nest new shoppingAPI
Enter fullscreen mode Exit fullscreen mode
  • CLI creates an App using Typescript as the default language if you like Javascript as the language you can run the below command.
nest new shoppingAPI -l JS
Enter fullscreen mode Exit fullscreen mode

Running and Testing API

Once a new project is created, we can use the below command to run the application.

cd shopping-API
npm start
Enter fullscreen mode Exit fullscreen mode

The App is configured to run on the port 3000 by default. and one controller is already defined visit http://localhost:3000/ and you will get Hello World! as a response.
We have created our first App using NestJS with minimum configuration, in the next article we will go through the App structure and will configure our own controller to handle the Http Requests.

Conclusion

NestJS is really easy to start with and if you have already used .Net, Java or even ExpressJS most of the concepts are similar. It offers CLI by using which we can easily scaffold our App and focus more on code. For an enterprise application, it becomes really easy to split the code across multiple modules, using NestJS modules.

Discussion (5)

Collapse
jcarlosweb profile image
Carlos Campos • Edited

Another option with Node+Express is github.com/TypedProject/ts-express...,
With TSED you don't need to create modules, so with NestJS it forces you to do it, which I don't understand and which makes the image put in Post look bad. Because with the controllers you have to create a module, while with TSED, you don't need that.

Collapse
santoshyadav198613 profile image
Santosh Yadav Author

The reason for modules is, the framework is heavily inspired by Angular, and I can understand many Javascript developers are not used to such pattern, but it makes structuring large apps really easy.

Collapse
jcarlosweb profile image
Carlos Campos

It would be ideal if NestJS did not force you to create modules, that is to say to have the option to create them or not.

Collapse
vinceramces profile image
Vince Ramces Oliveros

One thing that NestJS lacks is the deployment of their application.

architecture
Your diagram does not have Data Transfer Object(DTO) which is essential to have your Controller to validate(using class validator) before it gets to the Service Layer(which communicates to the Database).

Well, since it is just a "Getting Started" article, you might as well provide your DTO or the interface of that component in the next series.

Collapse
santoshyadav198613 profile image
Santosh Yadav Author

Yeah I will cover them in upcoming articles, here they are not mandatory. So skipped from the diagram.