NestJS 用TypeScript开发 nodeJS后端

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\nest\examples

框架源码
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 {}



实践

npm start 或者 npm run start
默认在 3000 端口
npm start

> nest-typescript-starter@1.0.0 start E:\NestJS\nest\examples\01-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

有请求路由的信息 , 好比
/cats



暂时没数据

要发POST请求建立数据 , 角色是 admin , 否则不能添加数据
@UseGuards(RolesGuard)

而后get再获取一次

Guards are executed after every middleware, but before pipes.

为何要用  Guards ? 以前已经用了 middleware






项目加载顺序
从左到右






相关文章
相关标签/搜索