Nest (2): Nest application directory structure and scaffolding command introduction

Introduction to Nest app directory structure and scaffolding commands

Before officially using NestJS for development, let's first understand the directory structure of Nest applications and some commonly used script commands.

Project directory

Below is the directory structure for a Nest project created using @nest/cli.

image-20230115144740250

The previous article introduced the src directory and the functions of each file under the directory. The following are descriptions of the other directories and files:

directory/file illustrate
test directory store test files
.eslintrc.js Configuration file for ESLint
.gitignore Used to configure files that do not need to be tracked by Git
.prettierrc Prettier's configuration file
nest-cli.json Configuration files for Nest projects
package.json Record project information, such as dependencies, scripts commands, etc.
pnpm-lock.yaml Record and lock the version information of the installed dependencies
tsconfig.build.json Configuration files for TypeScript syntax builds
tsconfig.json The configuration file of the TypeScript compiler, which is used to set some options at compile time

Introduction to the scripts command

Open package.jsonthe file, and @nest/clithe created project provides 12 scriptscommands. We need to understand several of the most commonly used commands, as follows:

 "scripts": {
    
    
    "build": "nest build",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
  },
  • build: Package the code suitable for the production environment.
  • start: Starts the Nest app during development. When using this command, you need to manually restart the service after modifying the code.
  • start:dev: with the watch option, that is, the service will be automatically restarted after the code is modified, so this script is generally used in the development stage.
  • start:prod: It actually starts the entry file of a build product with the node command.
  • start:debug: It has a debug option for code debugging.

@nestjs/cli common command introduction

@nestjs/cli is a very useful scaffolding tool officially provided by NestJS. Execute the following command to see what useful commands @nestjs/cli provides for developers.

nest --help

image-20230113173106756

  • nest new: Create a Nest project, which has already been experienced.
  • nest build: Build the application and package the source code as the code used in the production environment.
  • nest start: Run the application, used in the development phase.
  • nest generate: Quickly generate a Nest element.

nest generate

What is the Mest element? The front end uses HTML elements to build pages, and HTML elements are tags one by one. The same is true for Nest elements. To form a Nest application, you need to use Controller controller , Service service, Module module , Filter filter , Pipe pipeline , etc. These things are the elements of Nest.

In the previous article, the sample code of Hello, World was analyzed, modules, controllers and services were mentioned. As for what the remaining Nest elements are, what their meanings and functions are, they will be revealed in later articles.

The command nest generate | gcan be used to generate these Nest elements, it will give a template, developers don't have to manually build these basic structures.

Here's an example.

generate user module

$ nest g module user
CREATE user/user.module.ts (81 bytes)
UPDATE app.module.ts (309 bytes)

userA directory and user.module.tsmodule file will be created under the src directory :

image-20230116161619947

Code for the generated user module:

import {
    
     Module } from '@nestjs/common';
import {
    
     UserController } from './user.controller';

@Module({
    
    
  controllers: [UserController]
})
export class UserModule {
    
    }

When generating a new module, the scaffolding tool will automatically help complete the registration of the module:

// app.module.ts 根模块

import {
    
     UserModule } from './user/user.module';

@Module({
    
    
  imports: [UserModule],
  controllers: [AppController],
  providers: [AppService],
})

export class AppModule {
    
    }

Decorators @Modulehave a importsproperty that accepts other modules.

Generate user controller and service

$ nest g controller user
CREATE user/user.controller.ts (97 bytes)
CREATE user/user.controller.spec.ts (478 bytes)
UPDATE user/user.module.ts (166 bytes)

$ nest g service user
CREATE user/user.service.spec.ts (446 bytes)
UPDATE user/user.module.ts (240 bytes)

It will automatically find src/userthe directory and complete the creation of related files. When generating controllers and services, corresponding test files will be generated together.

image-20230116163115929

Controller's code:

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

@Controller('user')
export class UserController {
    
    }

The code for the service:

import {
    
     Injectable } from '@nestjs/common';

@Injectable()
export class UserService {
    
    }

Summarize

This article introduces the directory structure of the Nest application, focusing on several commonly used scripts commands, as well as generatethe commands provided by the @nest/cli scaffolding, which can save a lot of time in creating template code.

Thanks for reading, see you in the next article.

Guess you like

Origin blog.csdn.net/Old_Soldier/article/details/132408305