1. 使用CLI建立项目
1.1 安装nestjs@cli
npm i -g @nestjs/cli

1.2 新建项目
nest new 项目名称

现在我们新建了名为 geekjc的 nest项目,进入到geekjc目录下
cd geekjc
2. 开始hello,world.
通过cli生成的项目,已经为要跑Hello World!基本代码以及相关套件准备好了。

打开package.json,确定启动Server是哪一个script。
"scripts": {
...
// 一般启动
"start": "ts-node -r tsconfig-paths/register src/main.ts",
// src底下如果有文件名ts变更的話,重新启动Server。
"start:dev": "nodemon",
"start:debug": "nodemon --config nodemon-debug.json",
....
},
先跑一般启动
➜ geekjc npm run start
> geekjc@0.0.1 start /Users/chenlili/backend/geekjc
> ts-node -r tsconfig-paths/register src/main.ts
[Nest] 6330 - 2018-11-2 17:44:35 [NestFactory] Starting Nest application...
[Nest] 6330 - 2018-11-2 17:44:35 [InstanceLoader] AppModule dependencies initialized +12ms
[Nest] 6330 - 2018-11-2 17:44:35 [RoutesResolver] AppController {/}: +33ms
[Nest] 6330 - 2018-11-2 17:44:35 [RouterExplorer] Mapped {/, GET} route +2ms
[Nest] 6330 - 2018-11-2 17:44:35 [NestApplication] Nest application successfully started +2ms
nestjs app进入点在src/main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// Server接收port 3000的request
await app.listen(3000);
}
bootstrap();
Server跑在port 3000
打开chrome网址localhost:3000,画面如下


网友评论