NestJS
A progressive Node.js framework for building efficient and scalable server-side applications on top of TypeScript & JavaScript (ES6 / ES7 / ES8) heavily inspired by Angular
底层用 express
设计理念
这个后端框架受 Angular 启发 , 所以业务代码看起来和 Angular一样
完全基于typescript的web框架,完全oop风格
底层是 express,如果是koa,或者fastify的话,或许会比较火
2018-01-05
官网
https://nestjs.com/
Nest官方叫我们去看Angular 的依赖注入的文档
Nest is built around the strong design pattern commonly known as Dependency Injection. There's a great article about this concept in the official Angular documentation.
根据类型注入 , 不能根据 key
公司
已经很好用了 , 但是不确定是否这么强大 : 高级的功能
官方提供的ORM
分布式微服务
官方有提供 :
命令行工具
示例代码
https://github.com/nestjs/nest.git
E:NestJS
estexamples
框架源码
https://github.com/nestjs/nest.git
components 注解相当于 Spring 的 @Service
最后在模块里配置一下 , 相当于Spring 的 xml 配置
import { Module } from '@nestjs/common';
import { CatsController } from './cats/cats.controller';
import { CatsService } from './cats/cats.service';
@Module({
controllers: [CatsController],
components: [CatsService],
})
export class ApplicationModule {}
9
1
import { Module } from '@nestjs/common';
2
import { CatsController } from './cats/cats.controller';
3
import { CatsService } from './cats/cats.service';
4
5
({
6
controllers: [CatsController],
7
components: [CatsService],
8
})
9
export class ApplicationModule {}
实践
npm start 或者 npm run start
默认在 3000 端口
npm start
> nest-typescript-starter@1.0.0 start E:NestJS
estexamples 1-cats-app
> node index.js
[Nest] 12736 - 2018-1-5 09:11:29 [NestFactory] Starting Nest application...
[Nest] 12736 - 2018-1-5 09:11:29 [InstanceLoader] ApplicationModule dependencies initialized +7ms
[Nest] 12736 - 2018-1-5 09:11:29 [InstanceLoader] CatsModule dependencies initialized +2ms
[Nest] 12736 - 2018-1-5 09:11:29 [RoutesResolver] CatsController {/cats}: +29ms
[Nest] 12736 - 2018-1-5 09:11:29 [RouterExplorer] Mapped {/, POST} route +1ms
[Nest] 12736 - 2018-1-5 09:11:29 [RouterExplorer] Mapped {/, GET} route +0ms
[Nest] 12736 - 2018-1-5 09:11:29 [RouterExplorer] Mapped {/:id, GET} route +1ms
[Nest] 12736 - 2018-1-5 09:11:29 [NestApplication] Nest application successfully started +1ms
x
1
npm start
2
3
> nest-typescript-starter .0.0 start E:NestJS
estexamples 1-cats-app
4
> node index.js
5
6
[Nest] 12736 - 2018-1-5 09:11:29 [NestFactory] Starting Nest application...
7
[Nest] 12736 - 2018-1-5 09:11:29 [InstanceLoader] ApplicationModule dependencies initialized +7ms
8
[Nest] 12736 - 2018-1-5 09:11:29 [InstanceLoader] CatsModule dependencies initialized +2ms
9
[Nest] 12736 - 2018-1-5 09:11:29 [RoutesResolver] CatsController {/cats}: +29ms
10
[Nest] 12736 - 2018-1-5 09:11:29 [RouterExplorer] Mapped {/, POST} route +1ms
11
[Nest] 12736 - 2018-1-5 09:11:29 [RouterExplorer] Mapped {/, GET} route +0ms
12
[Nest] 12736 - 2018-1-5 09:11:29 [RouterExplorer] Mapped {/:id, GET} route +1ms
13
[Nest] 12736 - 2018-1-5 09:11:29 [NestApplication] Nest application successfully started +1ms
有请求路由的信息 , 比如
/cats
暂时没数据
要发POST请求创建数据 , 角色是 admin , 不然不能添加数据
@UseGuards(RolesGuard)
然后get再获取一次
Guards are executed after every middleware, but before pipes.
为什么要用 Guards ? 之前已经用了 middleware
项目加载顺序
从左到右