0
点赞
收藏
分享

微信扫一扫

nestjs项目初始化及常规操作

春意暖洋洋 2022-03-11 阅读 77

nestjs文档参考地址

本帖单纯记录自己安装本地开发环境的过程

项目起步安装

1、通过cli快速搭建项目

$ npm i -g @nestjs/cli
$ nest new project-name

2、nest 快速创建对应文件夹的操作 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.
  update|u [options]                              Update Nest dependencies.
  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   │ in          │ Generate an interceptor declaration          │
      │ interfaceinterface   │ 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                 │
      └───────────────┴─────────────┴──────────────────────────────────────────────┘

3、不创建测试文件的命令
--no-spec 添加该参数 则在命令行创建文件的时候 不会自动生成测试文件 xx.controller.spec

// 通过命令行新建module 会自动在app.module.ts中导入新建的module
nest g mo modules/user/user => src/user/user.module.ts
// 在有对应module的情况下 通过命令行创建控制器(controller) 会在module中自动导入controller 否则会在app.module.ts中导入
nest g co modules/user/user --no-spec => src/user/user.controller.ts
// 在有对应module的情况下 通过命令行创建服务层(service) 会在module中自动导入service 否则会在app.module.ts中导入
nest g s modules/user/user --no-spec => src/user/user.service.ts
举报

相关推荐

0 条评论