Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Introduction to NestJS

1. Environment

This tutorial is written using the following environment:

  • Hardware: Slimbook Pro 13.3 “(Intel Core i7, 32GB RAM)
  • Operating System: LUbuntu 16.04
  • Visual Studio Code 1.16.1
  • NodeJS v9.3.0
  • NestJS v4.5.10
  • Docker Compose 1.16.1
  • Docker 18.02.0-ce

2. Introduction

The world of consulting if something good is that it allows you to know and work with a lot of methodologies, technologies and tools of different nature:
front, back, devops, agile …

Currently the coding technologies of both the back and the front are converging and the concepts that you have to take into account both in one and in another are becoming more similar, in large part because the front has been ordered and humanized after the arrival of TypeScript.

Now a full stack quite comfortable to follow by a developer is the binomial Java (Spring Boot) – Angular that both are asked in job offers.

So far the biggest gap was when the front was in Angular (with its injection of dependencies, its decorators, its structure, …) and the back you had to do it in NodeJS, usually with Express for the creation of an API Rest and Socket. io to work with Web Sockets, by default, implementing with JavaScript. This has the advantage of having a single runtime (NodeJS) and simplifying the management of the configuration of the projects as well as the processes of continuous integration; but it also makes a single developer have to change a lot of chip to keep the two layers.

I say so far because now we have NestJS which is to NodeJS what Spring is to Java; a framework on NodeJS in TypeScript that abstracts you from the use of Express and Socket.io through decorators, has dependency injection “inspired” in Angular and allows modularizing our applications applying concepts of object orientation and functional and reactive programming.

The official documentation is another of its strengths that you can find here and there is an official repository with many teaching examples .

3. First steps

To start “cacharrear” with the framework I recommend this repo that I have created, based on the repo that comes in the documentation but with the necessary configuration for the testing.

$> git clone https://github.com/raguilera82/nestjs-seed.git tutorial-nest
$> cd tutorial-nest
$> npm install
$> npm run start

In this repository the unit and integration tests are configured with Jest and we can launch them with the command:

To start the application we simply have to execute:

That’s it, now if you access the URL: http: // localhost: 3000 you’ll have to see the message “NestJS Rocks” on the screen.

Perfect! We have already configured the project. Now we are going to open it with our favorite editor, in my case Visual Studio Code.

$> code .

Let’s see why that text has been painted on screen. If we go to the file “src / main.ts” we see the start of the application and where we can configure so that the project starts on a port other than 3000.

In the file “src / app.Module.ts” we have the definition of the main module that is referenced in the file “src / main.ts” and that, among other elements, defines the controllers that our project will have .

So any change in the code will cause the automatic reload of the application. To do a quick test simply modify the text “NestJS Rocks” by “NestJS loves Angular”, you will see in the terminal that the project is recompiled only and, when you refresh the browser, the new text appears.

All right! Bad pint does not have the environment.

4. REST API case study with access to database

Let’s see how easy it is to make a typical case of database access to return the records in JSON format from scratch and with unit tests.

All the code and the environment you have prepared and ready to run in the repository: https://github.com/raguilera82/tutorial-nest

Before getting into the code we are going to prepare the database so we use Docker and Docker Compose and simply execute:

This will lower the MySQL image, create the database with the “Tutorials” table populated with test data and leave the instance listening to port 3306. (Inside docker-compose.yml you can see the access data)

To access the MySQL database we will use TypeORM so we need to include the following dependencies:

Now we create the TypeORM configuration file called “ormconfig.json” with the access configuration to the database and where the entities of the application reside:

{

“type”: “mysql”,
“host”: “localhost”,
“port”: 3306,
“username”: “dba”,
“password”: “password”,
“database”: “testdb”,
“entities”: [“src/**/**.entity{.ts,.js}”],
“synchronize”: true

}

Now we set this configuration in the application by editing the file “src / app.module.ts” to add the module of TypeORM.

And then we create the fake service (“src / tutorials / tutorials.service.fake.ts”) that returns the response,
where we do not have to reference TypeORM
.

import { TUTORIALS_DATA } from ‘./tutorials.data’;
import { Component } from ‘@nestjs/common’;

import { Tutorial } from ‘./tutorial.entity’;

@Component()
export class TutorialsServiceFake {
constructor() {}

async findAll(): Promise {
return await TUTORIALS_DATA;
}
}

In this way the controller test is much simpler because thanks to the injection of dependencies, as we also do in Angular, we can change the real implementation, since for the controller I do not care where the data comes from but I do with them .

You can see that the test runs correctly.

Once this is done, it only remains to register the elements in the corresponding modules. In tutorials.module.ts we register the controller and the service with the real implementation.

And navigating to the URL http: // localhost: 3000 / tutorials we will see the list of tutorials registered in the database.

5. Conclusions

This is just a small case of very common use that can be solved with this framework, I recommend that you take a look at the official documentation to see its full potential.

The stack NestJS in the back and Angular in the front does not have to change the chip much to change between the two worlds,
which detracts from mysticity of the fullstack developer

The post Introduction to NestJS appeared first on Target Veb.



This post first appeared on Targetveb, please read the originial post: here

Share the post

Introduction to NestJS

×

Subscribe to Targetveb

Get updates delivered right to your inbox!

Thank you for your subscription

×