Nest (NestJS)是一个用于构建高效、可伸缩的 Node.js 服务器端框架。
NestJS 默认使用 JavaScript 的超集 TypeScript
进行开发。
环境准备
查看node和npm版本:
$ node --version
v15.2.1
$ npm --version
7.0.14
安装 @nestjs/cli
使用 npm 全局安装 @nestjs/cli
:
$ npm i -g @nestjs/cli
/usr/local/bin/nest -> /usr/local/lib/node_modules/@nestjs/cli/bin/nest.js
in/nest.js
+ @nestjs/cli@7.5.3
added 3 packages from 3 contributors and updated 10 packages in 39.209s
使用 nest --version
命令查看 nest 当前版本:
$ nest --version
7.5.3
使用 nest new
命令创建一个名为 nest-api
的项目:
$ nest new nest-api
⚡ We will scaffold your app in a few seconds..
CREATE nest-api/.eslintrc.js (630 bytes)
CREATE nest-api/.prettierrc (51 bytes)
CREATE nest-api/README.md (3339 bytes)
CREATE nest-api/nest-cli.json (64 bytes)
CREATE nest-api/package.json (1962 bytes)
CREATE nest-api/tsconfig.build.json (97 bytes)
CREATE nest-api/tsconfig.json (339 bytes)
CREATE nest-api/src/app.controller.spec.ts (617 bytes)
CREATE nest-api/src/app.controller.ts (274 bytes)
CREATE nest-api/src/app.module.ts (249 bytes)
CREATE nest-api/src/app.service.ts (142 bytes)
CREATE nest-api/src/main.ts (208 bytes)
CREATE nest-api/test/app.e2e-spec.ts (630 bytes)
CREATE nest-api/test/jest-e2e.json (183 bytes)
? Which package manager would you ❤️ to use? npm
▸▹▹▹▹ Installation in progress... ☕
🚀 Successfully created project nest-api
👉 Get started with the following commands:
$ cd nest-api
$ 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
启动项目
进入项目,并启动项目
$ cd nest-api
$ npm run start
> nest-api@0.0.1 start
> nest start
[Nest] 95920 - 2020/12/02 下午4:17:34 [NestFactory] Starting Nest application...
[Nest] 95920 - 2020/12/02 下午4:17:34 [InstanceLoader] AppModule dependencies initialized +18ms
[Nest] 95920 - 2020/12/02 下午4:17:34 [RoutesResolver] AppController {}: +9ms
[Nest] 95920 - 2020/12/02 下午4:17:34 [RouterExplorer] Mapped {, GET} route +4ms
[Nest] 95920 - 2020/12/02 下午4:17:34 [NestApplication] Nest application successfully started +3ms
查看启动效果:
(1)打开浏览器,访问 http://localhost:3000/
就可以看到页面输出了 Hello World!
文字。
(2)在终端使用 curl
请求:
$ curl -i localhost:3000
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 12
ETag: W/"c-Lve95gjOVATpfV8EL5X4nxwjKHE"
Date: Wed, 02 Dec 2020 08:18:21 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Hello World!%
可以看到, 输出了 Hello World!
字符串。
使用 -i
参数,表示要输出 header 头信息。
在 header 头信息中,我们可以看到,X-Powered-By
值为 Express
, 就是告诉我们这个网站或框架底层是 Express
框架。
Express 是一个基于 Node.js 平台,快速、开放、极简的 Web 开发框架。
项目结构
查看一下项目结构:
$ ll nest-api
total 1432
-rw-r--r-- 1 wangtom staff 3.3K 12 2 16:14 README.md
drwxr-xr-x 15 wangtom staff 480B 12 2 16:17 dist/
-rw-r--r-- 1 wangtom staff 64B 12 2 16:14 nest-cli.json
drwxr-xr-x 593 wangtom staff 19K 12 2 16:15 node_modules/
-rw-r--r-- 1 wangtom staff 695K 12 2 16:15 package-lock.json
-rw-r--r-- 1 wangtom staff 1.9K 12 2 16:15 package.json
drwxr-xr-x 7 wangtom staff 224B 12 2 16:14 src/
drwxr-xr-x 4 wangtom staff 128B 12 2 16:14 test/
-rw-r--r-- 1 wangtom staff 97B 12 2 16:14 tsconfig.build.json
-rw-r--r-- 1 wangtom staff 339B 12 2 16:14 tsconfig.json
可以使用 tree 命令查看项目的目录结构:
比如使用 tree ./nest-api -L 1
, 查看1级目录结构。
<