初识Nest.js
1. 全局安装Nestjs/cli
使用命令:
npm i -g @nestjs/cli
2. 验证安装
使用命令:
wujiayudeMacBook-Pro:~ wjy$ nest -V
6.3.0
3. 创建一个Nest项目
使用命令:
nest new hello-nest
这里的hello-nest是项目名称
在创建的过程中会遇到选择使用哪种NodeJS一起安装的包管理工具,在这里选择使用npm;
然后就是等啊等…
wujiayudeMacBook-Pro:test wjy$ nest new hello-nest
⚡ We will scaffold your app in a few seconds..
CREATE /hello-nest/.prettierrc (51 bytes)
CREATE /hello-nest/README.md (3370 bytes)
CREATE /hello-nest/nest-cli.json (84 bytes)
CREATE /hello-nest/nodemon-debug.json (163 bytes)
CREATE /hello-nest/nodemon.json (132 bytes)
CREATE /hello-nest/package.json (1665 bytes)
CREATE /hello-nest/tsconfig.build.json (89 bytes)
CREATE /hello-nest/tsconfig.json (300 bytes)
CREATE /hello-nest/tslint.json (426 bytes)
CREATE /hello-nest/src/app.controller.spec.ts (617 bytes)
CREATE /hello-nest/src/app.controller.ts (274 bytes)
CREATE /hello-nest/src/app.module.ts (249 bytes)
CREATE /hello-nest/src/app.service.ts (142 bytes)
CREATE /hello-nest/src/main.ts (208 bytes)
CREATE /hello-nest/test/app.e2e-spec.ts (561 bytes)
CREATE /hello-nest/test/jest-e2e.json (183 bytes)
? Which package manager would you ❤️ to use? npm
✔ Installation in progress... ☕
? Successfully created project hello-nest
? Get started with the following commands:
$ cd hello-nest
$ npm run start
Thanks for installing Nest ?
Please consider donating to our open collective
to help us maintain this package.
? Donate: https://opencollective.com/nest
这个时候,一个nest的项目就新建成功了~
4. 启动这个项目
- 切换到项目目录
cd hello-nest/
- 启动
npm run start
然后会出现:
wujiayudeMacBook-Pro:hello-nest wjy$ npm run start
> hello-nest@0.0.1 start /Users/wjy/WebstormProjects/hello-nest
> ts-node -r tsconfig-paths/register src/main.ts
[Nest] 11896 - 2019/04/11 下午3:28 [NestFactory] Starting Nest application...
[Nest] 11896 - 2019/04/11 下午3:28 [InstanceLoader] AppModule dependencies initialized +13ms
[Nest] 11896 - 2019/04/11 下午3:28 [InstanceLoader] AngularModule dependencies initialized +2ms
[Nest] 11896 - 2019/04/11 下午3:28 [RoutesResolver] AppController {/api/}: +5ms
[Nest] 11896 - 2019/04/11 下午3:28 [RouterExplorer] Mapped {/, GET} route +3ms
[Nest] 11896 - 2019/04/11 下午3:28 [NestApplication] Nest application successfully started +4ms
然后打开浏览器输入:http://localhost:3000/ ,就会看到如下: