Introduction to NestJS Part 1

Introduction

At 5mart Studio we use Javascript for most of our projects. At the same time, we shouldn’t forget about the fact that Javascript is not a high-level programming language, which implies we can easily run into errors that we would never experience while using a high-level programming language. In order to correct or to find the root cause of these errors people usually need to put tremendous hours in. Our solution to correct these errors is definitely Typescript. Although many would bet on the combination of Javascript + Flow just to spare some time, and to avoid errors, we would still choose Typescript. When we started using Typescript we could easily discover its advantages, its reliability and the sheer fact that in the long run we would just definitely benefit from using it for programming. That’s the reason we started thinking about not only using Typescript in React but also switching from the Spring Boot framework on the backend to Typescript. Long story short, that’s how we found the NestJS framework.

What is NestJS?

NestJS is a NodeJS framework entirely composed in Typsecript, which we could now use on an ExpressJS basis that is acknowledged by many great companies, or on a quite newby but still promising and faster Fastify basis. The framework tends to smartly build CLI, DI (Dependency Injection), Modular architecture, GraphQL / REST support, Microservice architecture and supports many database connections such as Postgres(SQL) or Mongo(NoSQL)

Why NestJS?

NestJS truly gives us a unique opportunity regardless of the size of our project. When we are ahead of a tiny or medium project we can create a simple REST API with it but still on a secure, certain and scalable basis. At the same time, NestJS even gives a helping hand when it comes to the project of your life, since it supports the Microservice architecture perfectly.

Fundamentals

Starting a new project

Fundamentals of NestJS

In a quite recent version, NestJS got a completely new CLI command:

Modules of NestJS

A NestJS module is basically a class, which is annotated by the @Module() decorator function.

Modules in NestJS
import { Module } from '@nestjs/common';
import { DogsController } from './dogs.controller';
import { DogsService } from './dogs.service';

@Module({
imports: [],
exports: [DogsService],
controllers: [DogsController],
providers: [DogsService],
})
export class DogsModule {}
import { Module } from '@nestjs/common';
import { DogsModule } from '../dogs/dogs.module';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';

@Module({
imports: [DogsModule], // UsersService can now access the DogsService because the DogsService is exported in the DogsModule, and the DogsModule is imported in the UsersModule.
exports: [UsersService],
controllers: [UsersController],
providers: [UsersService],
})
export class UsersModule {}

NestJS Controller

Controllers practically work the same as in every other web-based framework. They process the incoming request and give answers based on the evaluation of the requests, even if it is a result or an error.

import { Controller, Get } from '@nestjs/common';

@Controller('dogs')
export class DogsController {
@Get()
findAll(): string {
return 'This action returns all dogs';
}
}

NestJS Service

A service (provider) is a class that has to be annotated by the @Injectable() decorator function.

import { Injectable } from '@nestjs/common';
import { Dog } from './interfaces/dog.interface';

@Injectable()
export class DogsService {
private readonly dogs: Dog[] = [];

create(dog: Dog) {
this.dogs.push(dog);
}

findAll(): Dog[] {
return this.dogs;
}
}