|
| 1 | +NestJS 备忘清单 |
| 2 | +=== |
| 3 | + |
| 4 | +[NestJS](https://docs.nestjs.com/) 是一个用于构建高效、可扩展的 Node.js 服务器端应用程序的开发框架 |
| 5 | + |
| 6 | +创建应用 |
| 7 | +--- |
| 8 | + |
| 9 | +### NestCLI |
| 10 | + |
| 11 | +[NestJS](https://docs.nestjs.com/) 需要 [Node.js >= 12](https://nodejs.org) |
| 12 | + |
| 13 | +```bash |
| 14 | +npm i -g @nestjs/cli |
| 15 | +nest new project-name |
| 16 | +``` |
| 17 | + |
| 18 | +[Nest CLI](https://docs.nestjs.com/cli/overview) 是一个命令行界面工具,可以帮助你初始化、开发和维护你的Nest应用程序 |
| 19 | + |
| 20 | +```bash |
| 21 | +⚡ We will scaffold your app in a few seconds.. |
| 22 | + |
| 23 | +? Which package manager would you ❤️ to use? |
| 24 | + - npm |
| 25 | + - yarn |
| 26 | + - pnpm |
| 27 | +``` |
| 28 | + |
| 29 | +安装依赖并启动开发服务器 |
| 30 | + |
| 31 | +```bash |
| 32 | +cd <your-project-name> |
| 33 | +npm run start |
| 34 | +``` |
| 35 | + |
| 36 | +当你准备将应用发布到生产环境时,请运行: |
| 37 | + |
| 38 | +```bash |
| 39 | +$ npm run build |
| 40 | +``` |
| 41 | + |
| 42 | +此命令会在 `./dist` 文件夹中为你的应用创建一个生产环境的构建版本 |
| 43 | + |
| 44 | +### CLI指令 |
| 45 | + |
| 46 | +Command | Alias | Description |
| 47 | +:-|-|- |
| 48 | +| Command | Alias | Description |
| 49 | +| `new` | n | 使用模板快速创建应用 |
| 50 | +| `generate` | g | 自动生成`Controller`、`Providers` 和 `Modules` |
| 51 | +| `build` | | 打包并输出./dist目录 |
| 52 | +| `start` | | 打包并运行 |
| 53 | +| `add` | | 安装一个符合Nest的库,同`npm install` |
| 54 | +| `info` | i | 输出系统信息、CLI版本和Nest Package信息 |
| 55 | + |
| 56 | +### Platform(平台) |
| 57 | + |
| 58 | +目前`NestJS`支持两个`Node HTTP`平台:[Express](https://expressjs.com/) 和 [Fastify](https://www.fastify.io/)。从技术上讲,一旦创建了适配器,Nest 便可以使用任何 Node HTTP 框架 |
| 59 | + |
| 60 | +#### platform-express |
| 61 | + |
| 62 | +```ts |
| 63 | +import { NestFactory } from '@nestjs/core' |
| 64 | +import { AppModule } from './app.module' |
| 65 | +import { NestExpressApplication } from '@nestjs/platform-express' |
| 66 | + |
| 67 | +async function bootstrap() { |
| 68 | + const app = await NestFactory.create<NestExpressApplication>(AppModule) |
| 69 | + await app.listen(3000) |
| 70 | +} |
| 71 | +bootstrap() |
| 72 | +``` |
| 73 | + |
| 74 | +#### platform-fastify |
| 75 | + |
| 76 | +```ts |
| 77 | +import { NestFactory } from '@nestjs/core' |
| 78 | +import { AppModule } from './app.module' |
| 79 | +import { NestFastifyApplication } from '@nestjs/platform-fastify' |
| 80 | + |
| 81 | +async function bootstrap() { |
| 82 | + const app = await NestFactory.create<NestFastifyApplication>(AppModule) |
| 83 | + app.enableCors() // 开启Cors |
| 84 | + app.register(fastifyCsrf) |
| 85 | + await app.listen(4000, '0.0.0.0') |
| 86 | + |
| 87 | + console.log(`Application is running on: ${await app.getUrl()}`) |
| 88 | +} |
| 89 | +bootstrap() |
| 90 | +``` |
| 91 | + |
| 92 | +### 目录 |
| 93 | + |
| 94 | +``` |
| 95 | +├── src # 源代码目录 |
| 96 | +│ ├── app.module.ts // 应用程序的根模块 |
| 97 | +| └── app.controller.spec.ts // 控制器的单元测试 |
| 98 | +| ├── app.controller.ts // 单个路由的基本控制器 |
| 99 | +| └── app.service.ts // 具有单一方法的基本服务 |
| 100 | +| └── main.ts // 应用程序的入口文件,它使用核心函数 NestFactory 来创建 Nest 应用程序的实例 |
| 101 | +| |
| 102 | +└── test # 测试目录 |
| 103 | + ├── app.e2e-spec.ts |
| 104 | + └── jest-e2e.json |
| 105 | +``` |
| 106 | + |
| 107 | +### JavaScript |
| 108 | + |
| 109 | +`NestCLI` 默认是使用`TypeScript`进行初始化项目的,如果需要使用`JavaScript`请使用以下指令 |
| 110 | + |
| 111 | +```bash |
| 112 | +git clone https://github.com/nestjs/javascript-starter.git |
| 113 | + |
| 114 | +cd <javascript-starter> |
| 115 | +npm install |
| 116 | +npm run start |
| 117 | +``` |
| 118 | + |
| 119 | +需要[注意]((https://docs.nestjs.com/first-steps#language))的一点是,`JavaScript`的版本是需要`Babel`的 |
| 120 | + |
| 121 | +另见 |
| 122 | +--- |
| 123 | + |
| 124 | +[NestJs 官方文档](https://docs.nestjs.com/) |
0 commit comments