第一章 快速掌握 Nest CLI

1、nest 全局安装 指令

npm install -g @nestjs/cli

2、升级全局安装的nest版本 指令

有时候如果你想用最新的nest版本 ,但发现目前版本不是最新的 就可执行下面指令将全局nest版本更新到最新版本

npm update -g @nestjs/cli

3、nest项目创建指令

nest new 项目名

当然你也可以不用全局安装 直接使用下面指令创建项目 但还是推荐全局安装

npm install -g @nestjs/cli

4、nest cli指令

4.1、nest new

nest new 是用来创建一个新的nest项目

我们可以通过 下面指令来查询nest new 有哪些选项

npm new -h

Generate Nest application.
Options:
–directory [directory] Specify the destination directory
-d, --dry-run Report actions that would be performed without writing out results. (default:
false)
-g, --skip-git Skip git repository initialization. (default: false)
-s, --skip-install Skip package installation. (default: false)
-p, --package-manager [packageManager] Specify package manager.
-l, --language [language] Programming language to be used (TypeScript or JavaScript) (default:
“TypeScript”)
-c, --collection [collectionName] Schematics collection to use (default: “@nestjs/schematics”)
–strict Enables strict mode in TypeScript. (default: false)
-h, --help Output usage information.

–directory [directory] 指定目标目录 用于指定项目创建的目录
-g, --skip-git 跳过git存储库初始化。(默认值:false)
-s, --skip-install 跳过安装包。(默认值:false)
-p,–package-manager [packageManager] 指定包管理器。
-l,–language [language] 要使用的编程语言(TypeScript或JavaScript)(默认 TypeScript)
-c,–collection [collectionName] Schematics收集使用(默认:"@nestjs/ Schematics ")
–strict 在TypeScript中启用严格模式。(默认值:false)
-h,-help 输出使用信息。

4.2、nest generate

nest generate 用于生成项目的各种代码 例如: controller、service、module 等

1、nest generate module

以生成 module 为例子:

nest generate module 名称

1716619811333.png
将会生成 module 的代码
c591794dbfe1f7091f1172a1ff60aba.png
且在 AppModule 里自动引入
1716619941868.png

2、nest generate controller / nest generate service

当然也可以生成 controller、service 代码 例如下面代码:

nest generate controller 名称
nest generate service 名称

1716620148846.png
1716620171663.png
同时controller 和 service 也会更新到 module (test.module.ts)的依赖里去
1716620264838.png

3、nest generate resource

当然也可以直接生成一整个模块

nest generate resource 模块名称

就等于下面的3行代码

nest generate module 名称
nest generate controller 名称
nest generate service 名称

它会让你选择是哪种代码,因为 nest 支持 http、websocket、graphql、tcp 等,这里我们选择 http 的 REST 风格 api
1716620674663.png
是否生成 CRUD 代码
1716620721032.png
选择是
1716620801209.png
将自动生成整个模块 并自动生成CRUD代码
1716620889396.png

4、nest generate 常用命令

4.1、–flat 和 --no-flat

–flat 和 --no-flat 是指定是否生成对应目录 一般我们不指定的时候默认为生成对应目录 例如
1716621269706.png
可以看到生成的目录结构不一样 --no-flat 和默认 会生成 www文件夹(目录),而 --flat 不会生成文件夹(目录)

4.2、–skip-import

–skip-import 是指定不在 AppModule 里引入
例如:
1716621635576.png
不使用 --skip-import 时可以看到 src/app.model.ts 被更新了 说明被引入了 ,但是使用了 --skip-import 时只有创建 没有更新src/app.model.ts 说明没有被引入

4.3、–spec 和 --no-spec

–spec 和 --no-spec 是指定是否生成测试文件
例如:
1716621923534.png
还有更多的指令 可以执行下面代码查看所有指令:

nest generate -h

1716622004908.png

4.3、nest build

nest build 是用来构建项目(通俗一点的说就是打包项目),执行 nest build,会在 dist 目录下生成编译后的代码。它有着以下选项

-c, --config [path] Path to nest-cli configuration file.
-p, --path [path] Path to tsconfig file.
-w, --watch Run in watch mode (live-reload).
-b, --builder [name] Builder to be used (tsc, webpack, swc).
–watchAssets Watch non-ts (e.g., .graphql) files mode.
–webpack Use webpack for compilation (deprecated option, use --builder instead).
–type-check Enable type checking (when SWC is used).
–webpackPath [path] Path to webpack configuration.
–tsc Use typescript compiler for compilation.
–preserveWatchOutput Use “preserveWatchOutput” option when using tsc watch mode.
-h, --help Output usage information.

-c, --config [path] nest-cli配置文件路径
-p, --path [path] tsconfig文件的路径
-w, --watch 是监听文件变动,自动 build 的
-b, --builder [name] 要使用的构建器(tsc, webpack, swc)。
–watchAssets 注意非ts(例如,.graphql)文件模式
–webpack 使用webpack进行编译(已弃用选项,请使用——builder代替)
–type-check 启用类型检查(当使用SWC时)
–webpackPath [path] webpack配置的路径。
–tsc 使用typescript编译器进行编译

4.4、nest start

nest start 是用于启动项目
–watch 是最常用的选项,改动文件之后自动重新 build:

nest start --watch

1716619270536.png

nest start --debug

–debug 是启动调试的 websocket 服务,用来 debug。
906bca8378ee1d75dde583faee974d1.png
–exec 可以指定用什么来跑,默认是用 node 跑,你也可以切换别的 runtime。

4.5、nest info

nest info 查看项目信息的,包括系统信息、 node、npm 和依赖版本

  • 36
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枫ゞ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值