JavaScript is one of the most popular programming languages, with fast growth in the community. You can take a look at this stackoverflow survey 2021 to see how JS compares with other programming languages. JavaScript can help you become a very fast-working developer using its framework, and NestJs is an option for you to consider as a progressive JS developer.
On the official website (https://nestjs.com/), NestJs is defined as a framework for building efficient and scalable NodeJs server side applications. Nest is for building the backend of an app. NestJs is more efficient when we build an app backend from scratch since it enables us to move fast and focus on building functionality.
There are other frameworks for building on the backend like SailsJs, KoaJs, and the ever-popular ExpressJs. Most developers know about Express, it’s a bigger and older node js framework, and with it we can use express-generator to make initialization of a project easier. This npmtrends information shows you that Koa and Nest are not so different. Now let’s move into detail about NestJs.
Creating a new project with NestJs is very simple, you can refer to and follow the steps in the official documentation. I prefer to use NestJs CLI, it will make your life easier with a run of this command:
npm i -g @nestjs/cli
Then you can run the nest command to create a new project:
nest new project-name
You will be asked to select the package manager that you want to use:
Open your new project with an editor like VSCode. You should have a directory like this:
Our playground is on src directory, please run the command and you will see on browser http://localhost:3000/
npm run start
With NestJs we work per module. Inside the module directory we have controller, service, entity and other. Usually in an application we have a module directory, for example, we generate a User module, Product module, and Sales module. Inside each file we will find many useful decorators to make us faster when writing code.
We can get all this simply with Nest CLI generator. Try to run this command:
nest g resource users
You will be asked to define the type of module, you’ll want to use GraphQL or RestAPI.
Then you need to confirm if you want to generate CRUD entry points or not, I prefer to use them.
When we finish and do the same with the products and sales modules, we will get a directory like this:
That is a simple way to make a module. If you open a controller you will see the default CRUD entry point. Also you do not need to define routes manually and you can call URLs with the module name, which is more efficient. For another usage of NestCLI take a look here.
Another great thing about NestJs is that there are many features that can be easily integrated.
1. TypeORM
When you need to process backend data on the database, NestJs has some options like Sequelize or mongoose, but TypeORM is the preferred one to use. TypeORM is an object-relational mapping library that runs on NodeJs with many useful features and is an easy way to integrate with NestJs framework, check it out here.
2. GraphQL
GraphQL is a powerful query language for API. Many big companies use this now, and if you want a quick comparison with REST API you can read it here. GraphQL is very easy to integrate with NestJs.
3. OpenAPI
OpenAPI is a language-agnostic defined format for describing RESTful APIs. On NestJs you can use a simple decorator to define your entity and controller file, like ApiProperty, ApiResponse, ApiOperation. This simplifies work within our team.
There are many more things you can do with NestJs. Explore their official documentation and pick and choose what works for you. Thanks.