Nest.js我爱编程

1、Nest.js 项目搭建

2018-07-30  本文已影响2268人  RoyLin1996
Nest.js 5.0 系列教程。

什么是Nest.js?

Nest.js 是 Node.js 平台下,兼容 TypeScript 和 JavaScript 语法的高性能 Web开发框架。

默认框架底层 HTTP 实例是基于 Express 实现的,可以通过兼容的 HTTP 适配器进行修改。

支持 微服务 (Microservice) 和 web套接字 (WebSockets)

安装

需要 Node.js 版本 >= 6.11.0

使用 npm 安装 Nest CLI ,在终端中输入以下命令:

  $ npm i -g @nestjs/cli

检查 Nest 是否安装正常, 在终端中输入以下命令:

$ nest info

 _   _             _      ___  _____  _____  _     _____
| \ | |           | |    |_  |/  ___|/  __ \| |   |_   _|
|  \| |  ___  ___ | |_     | |\ `--. | /  \/| |     | |
| . ` | / _ \/ __|| __|    | | `--. \| |    | |     | |
| |\  ||  __/\__ \| |_ /\__/ //\__/ /| \__/\| |_____| |_
\_| \_/ \___||___/ \__|\____/ \____/  \____/\_____/\___/


[System Information]
OS Version     : Linux 4.15
NodeJS Version : v9.2.1
NPM Version    : 6.1.0
[Nest Information]
passport version : 1.1.0
common version   : 5.0.0
core version     : 5.0.0

安装缓慢可考虑更改 npm 镜像源,这里推荐使用 nrm , 好处是可以随时切换不同的源, 在终端中输入以下命令:

$ npm i -g nrm

$ nrm ls

  npm ---- https://registry.npmjs.org/
  cnpm --- http://r.cnpmjs.org/
* taobao - https://registry.npm.taobao.org/
  nj ----- https://registry.nodejitsu.com/
  rednpm - http://registry.mirror.cqupt.edu.cn/
  npmMirror  https://skimdb.npmjs.com/registry/
  edunpm - http://registry.enpmjs.org/

$ nrm use taobao

初始化项目

选择好工作目录,在终端中输入以下命令:

$ nest new project

生成的 src 目录中包含下面几个核心文件:

src
├──app.controller.ts    // 带有单个路由的基本控制器示例。
├── app.module.ts    // 定义 AppModule 应用程序的根模块。
└── main.ts    // 应用程序入口文件。它使用 NestFactory 用来创建 Nest 应用实例。

main.ts 中的内容,就是通过 NestFactory 工厂类的 create 方法,创建一个实现了 INestApplication 接口的服务实例:

import { NestFactory } from '@nestjs/core';
import { ApplicationModule } from './app.module';

async function bootstrap() {
    const app = await NestFactory.create(ApplicationModule);
    await app.listen(3000);
}

bootstrap();

更改依赖

可以采用手动传入实例的方式来更改 Nest 框架的底层依赖:

const server = express();
const app = await NestFactory.create(ApplicationModule, server);

运行多个服务

可以轻松创建多个服务器:

const httpsOptions = {
    key: fs.readFileSync("./secrets/private-key.pem"),
    cert: fs.readFileSync("./secrets/public-certificate.pem")
};

const server = express();
const app = await NestFactory.create(ApplicationModule, server);

await app.init();

http.createServer(server).listen(3000);
https.createServer(httpsOptions, server).listen(443);

运行多种服务

const app = await NestFactory.create(ApplicationModule);
const microservice = app.connectMicroservice({ transport: Transport.TCP });

await app.startAllMicroservicesAsync();
await app.listen(3001);

启动项目

在 package.json 的 npm scripts 中配置了框架的多种启动脚本:

"format": "prettier --write \"**/*.ts\"",    // 格式化 TypeScript 文件
"start": "ts-node -r tsconfig-paths/register src/main.ts",    // 启动项目
"start:dev": "nodemon",    // 使用 nodemon 启动开发环境
"prestart:prod": "rm -rf dist && tsc",    // 生产环境预启动
"start:prod": "node dist/main.js",    // 生产环境启动
"start:hmr": "node dist/server",    // 热重载启动
"test": "jest",    // 启动单元测试
"test:cov": "jest --coverage",    // 测试覆盖率
"test:e2e": "jest --config ./test/jest-e2e.json",    // 端到端测试
"webpack": "webpack --config webpack.config.js"    // 启动webpack 文件监听,用以配合热重载

使用以下脚本运行默认的HTTP服务器,在浏览器中访问 http://localhost:3000/ ,能够看到输出 Hello world!

$ npm run start

下一篇:2、Nest.js 创建基本的REST控制器

上一篇下一篇

猜你喜欢

热点阅读