• 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 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 {}



    实践

    npm start 或者 npm run start
    默认在 3000 端口
    npm start
    
    > nest-typescript-starter@1.0.0 start E:NestJS
    estexamples1-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






    项目加载顺序
    从左到右








  • 相关阅读:
    SyncNavigator V8.6.2数据库同步工具安装与卸载
    数据库同步软件|SyncNavigator数据库同步软件 V8.6.2官网下载地址
    sql server两个数据库同步
    怎么实现数据库异地同步备份
    两台服务器数据库同步
    如何实现数据实时同步
    数据库同步软件|SyncNavigator数据库同步软件 V8.6.2
    syncnavigator使用说明
    go 编译
    Go语言实现FastDFS分布式存储系统WebAPI网关
  • 原文地址:https://www.cnblogs.com/xiaoCong2016/p/8202485.html
Copyright © 2020-2023  润新知