美文网首页
一、nestjs cli的使用

一、nestjs cli的使用

作者: Nick_4438 | 来源:发表于2022-10-24 19:06 被阅读0次

简介

本文讲解如何快速使用nestjs

详细

安装cli,建立工程

npm i -g @nestjs/cli
# demo是项目名称
nest new demo
  • 创建项目架构如下:
.
├── README.md
├── nest-cli.json
├── package-lock.json
├── package.json
├── src
│   ├── app.controller.spec.ts
│   ├── app.controller.ts
│   ├── app.module.ts
│   ├── app.service.ts
│   └── main.ts
├── test
│   ├── app.e2e-spec.ts
│   └── jest-e2e.json
├── tsconfig.build.json
└── tsconfig.json
  • 使用下面命令,启动程序,启动后,可通过http://localhost:3000访问服务器
npm run start:dev

局部代码讲解

  • main.ts 入口
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();
  • app.service.ts
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
  getHello(): string {
    return 'Hello World!';
  }
  getName(): string {
    return 'Nick!';
  }
}

  • app.controller.ts 控制类
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  // 注入service
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }

  @Get('name/haha')
  getName(): string {
    return this.appService.getName();
  }
}

nestcli常用命令

  • help命令
nest --help
Usage: nest <command> [options]

Options:
  -v, --version                                   Output the current version.
  -h, --help                                      Output usage information.

Commands:
  new|n [options] [name]                          Generate Nest application.
  build [options] [app]                           Build Nest application.
  start [options] [app]                           Run Nest application.
  info|i                                          Display Nest project details.
  add [options] <library>                         Adds support for an external library to your project.
  generate|g [options] <schematic> [name] [path]  Generate a Nest element.
    Schematics available on @nestjs/schematics collection:
      ┌───────────────┬─────────────┬──────────────────────────────────────────────┐
      │ name          │ alias       │ description                                  │
      │ application   │ application │ Generate a new application workspace         │
      │ class         │ cl          │ Generate a new class                         │
      │ configuration │ config      │ Generate a CLI configuration file            │
      │ controller    │ co          │ Generate a controller declaration            │
      │ decorator     │ d           │ Generate a custom decorator                  │
      │ filter        │ f           │ Generate a filter declaration                │
      │ gateway       │ ga          │ Generate a gateway declaration               │
      │ guard         │ gu          │ Generate a guard declaration                 │
      │ interceptor   │ itc         │ Generate an interceptor declaration          │
      │ interface     │ itf         │ Generate an interface                        │
      │ middleware    │ mi          │ Generate a middleware declaration            │
      │ module        │ mo          │ Generate a module declaration                │
      │ pipe          │ pi          │ Generate a pipe declaration                  │
      │ provider      │ pr          │ Generate a provider declaration              │
      │ resolver      │ r           │ Generate a GraphQL resolver declaration      │
      │ service       │ s           │ Generate a service declaration               │
      │ library       │ lib         │ Generate a new library within a monorepo     │
      │ sub-app       │ app         │ Generate a new application within a monorepo │
      │ resource      │ res         │ Generate a new CRUD resource                 │
      └───────────────┴─────────────┴──────────────────────────────────────────────┘
  • 生成Controllerr
PS C:\Users\Administrator\Desktop\study\netstjs\ioc-test\05cli\demo> nest g co demo
CREATE src/demo/demo.controller.ts (97 bytes)
CREATE src/demo/demo.controller.spec.ts (478 bytes)
UPDATE src/app.module.ts (322 bytes)
  • 生成Module
PS C:\Users\Administrator\Desktop\study\netstjs\ioc-test\05cli\demo> nest g mo demo
CREATE src/demo/demo.module.ts (81 bytes)
UPDATE src/app.module.ts (381 bytes)
  • 生成Service
PS C:\Users\Administrator\Desktop\study\netstjs\ioc-test\05cli\demo> nest g s demo
CREATE src/demo/demo.service.ts (88 bytes)
CREATE src/demo/demo.service.spec.ts (446 bytes)
UPDATE src/demo/demo.module.ts (155 bytes)
  • 一条命令生成完整资源
PS C:\Users\Administrator\Desktop\study\netstjs\ioc-test\05cli\demo> nest g resource user
? What transport layer do you use? REST API
? Would you like to generate CRUD entry points? Yes
CREATE src/user/user.controller.ts (883 bytes)
CREATE src/user/user.controller.spec.ts (556 bytes)
CREATE src/user/user.module.ts (240 bytes)
CREATE src/user/user.service.ts (607 bytes)
CREATE src/user/user.service.spec.ts (446 bytes)
CREATE src/user/dto/create-user.dto.ts (30 bytes)
CREATE src/user/dto/update-user.dto.ts (169 bytes)
CREATE src/user/entities/user.entity.ts (21 bytes)
UPDATE package.json (2022 bytes)
UPDATE src/app.module.ts (442 bytes)
✔ Packages installed successfully.
  • 生成的请求

GET查询请求
POST提交插入请求
PUT PATCH 更新请求
DELETE删除请求

相关文章

网友评论

      本文标题:一、nestjs cli的使用

      本文链接:https://www.haomeiwen.com/subject/fzgizrtx.html