NestJS – the divine framework for Nodejs

Tram Ho

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 and Javascript thuần , the default is Typescript .
  • 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 to GraphQL , Websocket and cqrs 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.

nestjs / cli will genarate a structured project

Run your application with the command:

4. Go a little deeper

Create a new module:

The user-module file will look like this:

It will be imported into AppModule:

Now that you have a new module, create controllers and services for it:

The file service will look like this:

You can see @Injectable() to inject UserService, quite similar to Angular and Java Spring? 😀

Next is the controller:

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

Share the news now

Source : Viblo