Reading may sound headline but wait, things are not as you think, listen to me explain
1. Why is it NestJS?
In your opinion, what is the biggest problem of NodeJS
? Many will think of performance. But that really isn’t a big deal for NodeJS. NodeJS’s performance is not as bad as you think it is, but vice versa is quite good. Of course not comparable with C ++ or Go, Java. But it meets most of the needs of a backend system with millions of users.
In recent years, thanks to NodeJs
, JavaScript has become a great web language for both frontend and backend applications. NodeJS
helps improve developer productivity and allows creating applications quickly, testable and scalable. However, while there are many frameworks for NodeJS
, none of them effectively solves the main problem of NodeJS
– the issue of Architecture.
Nest (NestJS) is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with and fully supports TypeScript (yet still enables developers to code in pure JavaScript) and combining elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
Nest provides an out-of-the-box application architecture which allows developers and teams to create highly testable, scalable, loosely coupled, and easily maintainable applications. The architecture is heavily inspired by Angular.
This is what is written on the NestJS homepage, and you can get a rough NestJS
!
2. So what does NestJS have?
- NestJS is compatible with both
Typescript
andJavascript thuần
, the default isTypescript
. - The Design Pattern throughout NestJS is Dependency Injection, and the syntax of NestJS is very similar to Angular. So if you already have knowledge about Angular, getting started with NesJS will be quite easy.
- NestJS has many modules to support you, from
hot reload
,logger
toGraphQL
,Websocket
andcqrs
patterns,microservices
, … You just need NestJS to do everything. - The
microservices
module of NestJS supports all kinds of connections:RabbitMQ
,gRPC
,Kafka
,Redis
, … this is one of my favorite things, I just need to focus on the code for the business part, and infra, NestJS has already implemented just use.
3. First step
First, install @nestjs/cli
and initialize your project.
1 2 3 | $ npm i -g @nestjs/cli $ nest new project-name |
nestjs / cli will genarate a structured project
1 2 3 4 5 | src ├──app.controller.ts ├──app.module.ts ├──main.ts |
Run your application with the command:
1 2 | $ npm run start |
4. Go a little deeper
Create a new module:
1 2 | nest generate module user-module |
The user-module file will look like this:
1 2 3 4 5 | // user-module.module.ts import { Module } from ‘nestjs/common’; @Module({}) export class UserModule{} |
It will be imported into AppModule:
1 2 3 4 5 6 7 8 9 10 11 12 | //app.module.ts import { Module } from ‘@nestjs/common’; import { AppController } from ‘./app.controller’; import { AppService } from ‘./app.service’; import { UserModuleModule } from ‘./user-module/user-module.module’; @Module({ imports: [UserModuleModule], controllers: [AppController], providers: [AppService], }) export class AppModule {} |
Now that you have a new module, create controllers and services for it:
1 2 3 4 | $ cd src/user-module $ nest generate provider user-service $ nest generate controller user-controller |
The file service will look like this:
1 2 3 4 5 6 7 8 9 | // user-service.ts import { Injectable } from ‘@nestjs/common’; @Injectable() export class UserService { getUser(){ return “User”; } } |
You can see @Injectable()
to inject UserService, quite similar to Angular and Java Spring?
Next is the controller:
1 2 3 4 5 6 7 8 9 10 11 12 | // user-controller.controller.ts import { Controller,Get } from '@nestjs/common'; import {UserService} from '../user-service'; @Controller('user-controller') export class UserControllerController { constructor(private readonly service:UserService){} @Get() getUser(){ return this.service.getUser() } } |
So when accessing http: // localhost: 3000 / user-controller server will return “User” already.
5. Conclusion
This is just an introductory article about NestJS, a relatively new framework (released in 2017) that I think is worth exploring. Hopefully the article will help you have more choices for backend server