PART 1: Introduction and setup NestJS – NodeJS.

Tram Ho

Introducing NestJS.

  • NestJS is a framework developed on Nodejs platform.
  • Nodejs is Node.js is a platform that runs on the V7 JavaScript runtime environment. Therefore, Nodejs can work on both the client and the server.
  • But special of NestJS is that it is built in Typescript language, this is a combination of object-oriented thinking (OOP), functional programming thinking (functional programming).

Nest.js has a lot of features like:

  • Scalability: Thanks to the modular architecture, Nest allows you to use other existing libraries for your project.
  • Architecture: Nest has a design architecture for projet, offering easy testability, scalability and high maintainability.
  • Flexibility: Nest provides an ecosystem to build all kinds of applications for server side.
  • Progressivity: Nest uses the latest JavaScript features and implements complete solutions and designs in software development.
  • By using TypeScript and Angular background concepts, Angular developers can learn Nest.js very quickly, and write the backend right for the Angular app without using other server-side frameworks.

Express is a minimal and fast web framework for Node.js, providing many HTTP utilities to help you build powerful REST APIs easily and quickly. As for TypeORM, this is the most stable ORM (Object Relational Mapper) available for modern TypeScript and JavaScript languages. It has support for both Active Record and Data Mapper templates, allowing you to build high quality, loose, scalable and maintainable applications on the most popular database systems currently available. There are such as MySQL, PostgreQuery and Oracle.

Setting.

1: Before Use

To get started with Nest.js, you need a few prerequisites:

  • Node.js and NPM are already installed on the system, you can install both from the official website or follow the instructions in the documentation of the system you are using.
  • Proficiency in TypeScript. If you have experience developing with Angular, this condition will probably be obvious, because Angular is based on TypeScript.

2: install nestjs CLI.

Nest CLI is a command-line interface utility that allows you to quickly create projects from with the necessary base files and dependencies. With Nest CLI, you can also structure many different artifacts like components and modules; serving for the developing app and building the final app
Ready for production. The Nest CLI is based on the Angular Devkit pakage and uses nodemon to see changes made to the file.
Get started by installing Nest CLI. Open new terminal and run the following command:
npm install -g @nestjs/cli
Note: you may have to add sudo before the command (with macOS and Debian) or use an extra CMD prompt (with Windows). If you are not a superuser and still want to install globally, you need to edit the npm permissions first.
After installing CLI, you can quickly create a Nest.js project and develop it right away.

3: Create a Nest Project in Nest.js

After installing CLI, you can use it to quickly create projects. After installing CLI, open the terminal to run the following command:
nest new firstnestproject
CLI will ask you some information about your project, such as description, version and author. You can add these details or just leave them blank and press Enter.
The CLI will create a series of files and folders and then remind you of the package manager you want to use with your project. You can choose npm or yarm, in this tutorial we will use npm.
After successfully installing the necessary dependencies, navigate to the project’s root directory and run the following command to start the live-load server based on nodemon:
npm run start:dev
You can use the web browser to access the Nest server at http://127.0.0.1:3000/ . At this time, the page will only display Hello World !.
You can leave the server running and open the terminal to run another command we will cover in this tutorial.

4: Structure of Project!

Here is a screenshot of the project’s structure:
The project includes the node_modules folder and the package.json file, an essential component of any Node.js project. At the same time, you may also see:

  • The file tsconfig.json helps customize TypeScript
  • The nodemon.json file helps to customize the nodemon
  • The tslint.json file helps lint TypeScript
  • nest-cli.json helps customize CLI
  • The src / directory contains the project’s code
  • The test / folder contains the test file.

Epilogue

In this article, I introduce the framework Nest.js. How to create your controller and module. thanks!

Share the news now

Source : Viblo