This is the 29th day of my participation in the August Wenwen Challenge.More challenges in August

Environment and version introduction

  • Node: v14.17.0
  • Npm: 7.15.0
  • Yarn: 1.22.10
  • Nest: 8.0.2

thinking

Before learning, I also asked myself some questions about learning NestJS:

  • What is NestJS?
  • What are the advantages of NestJS?
  • Suitable for what scenarios?
  • Why use NestJS? What are the official ready-made solutions?

Let’s take the questions one by one to find the answer!

The problem

What is the

Nestjs is designed to build efficient, reliable, and progressive server-side applications.

advantage

  • Based on Node.js, front-end developers feel strangely familiar.
  • High cohesion low coupling, a set of mature development specifications.
  • TypeScript supports robust applications.
  • Ecologically active, with mature solutions out of the box; For example, authentication, logging, and security.

Application scenarios

  • Construction of large projects in Taiwan
  • Micro service
  • .

Basic environment construction

The node environment

If you do not have it installed, you can visit: long-term support is recommended on the official website

$ node -v/ / v14.17.0
$ npm -v/ / 7.15.0Copy the code

Installing the CLI Tool

If you are using a Mac, you need to add sodu in front of it and press Enter to enter the password of your laptop. You can’t see your password on the console when you enter it. If you have entered the correct password, you can install it globally. If you don’t add it, the global installation won’t work.

$ npm i -g @nestjs/cli
Copy the code

After the installation, run the following command to check the CLI version

$ nest -v/ / 8.0.2Copy the code

Initialize the project

There are two ways to initialize the project: one is to pull the official template, the other is to use the CLI tool to create the project (recommended).

Pull the template

// Pull the item$ git clone https://github.com/nestjs/typescript-starter.git project// Go to the project directory$ cd project// Install dependencies$ npm install// Run the project$ npm run start
Copy the code

After the project is successfully started, it is run on port 3000. Visit http://localhost:3000 in the browser

Scaffolding creation

$Nest new [project-name] // Example: Nest new learn
Copy the code

Once created, you can view the project directory and package.json

Project directory and configuration files

│ ├─ App.Controller.js │ ├─ App.Controller.js │ ├─ App.Controller.js App. The module. Which s │ ├ ─ app. The module. The js │ ├ ─ app. Module. Js. Map │ ├ ─ app. Service. Which s │ ├ ─ app. Service. Js │ ├ ─ App. Service. Js. Map │ ├ ─ the main, which s │ ├ ─ main. Js │ ├ ─ main. Js. Map │ └ ─ tsconfig. Build. Tsbuildinfo ├ ─ SRC / / project source directory │ ├ ─ App. Controller. Spec. Ts │ ├ ─ app. The controller. The ts │ ├ ─ app. The module. The ts │ ├ ─ app. Service. The ts │ └ ─ main. Ts ├ ─ the test / / │ ├─ ├─ ├.e2e-spec. Ts │ ├─ ├─ ├.eslintrc.js // Eslint ├─.gitignore // Git Version Control .prettierrc // Prettier ├─ nest-cli.json // cli Config file ├─ package.json // project Config file ├─ tsconfig.build.json ├─ ├─ damn.lockCopy the code

All commands in the scripts object in package.json:

/ /...
"scripts": {
    "prebuild": "rimraf dist".// Rebuild the command
    "build": "nest build".// Project package command
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"".// Format the processing command
    "start": "nest start".// Start the script command by default
    "start:dev": "nest start --watch".// Start command for code change monitoring at development time
    "start:debug": "nest start --debug --watch".// Start command for debugging
    "start:prod": "node dist/main".// Production environment start command
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix".// eslint checks repair commands
    "test": "jest".// Run unit tests
    "test:watch": "jest --watch".// Listen for unit test file changes
    "test:cov": "jest --coverage"."test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand"."test:e2e": "jest --config ./test/jest-e2e.json"
  },
/ /...
Copy the code

Execute the command to start the project:

$ yarn start
or
$ npm run start
Copy the code

After the service is successfully started; The program runs on port 3000 (you can also pass in a custom port in the app.listen() method in the SRC /main.ts file); Then ask in your browser: http://localhost:3000