五. 学点nestJS

188 阅读3分钟

GITHUB: github.com/ChengZi1011

一. nestJS简介

概念:

[Nestjs] 是一个用于构建高效可扩展的一个基于Node js 服务端 应用程序开发框架。并且完全支持typeScript,结合了 AOP 面向切面的编程方式。

nestjs 的底层代码运用了 express 和  Fastify 在他们的基础上提供了一定程度的抽象,同时也将其 API 直接暴露给开发人员。这样可以轻松使用每个平台的无数第三方模块。

官网

nest js 英文官网 NestJS - A progressive Node.js framework

nestjs 中文网  NestJS 简介 | NestJS 中文文档 | NestJS 中文网

nestjs 中文网2  Nest.js 中文文档

二. IOC控制反转和DI依赖注入

IOC(控制反转)是一种设计原则,DI(依赖注入)是IOC原则的实现方式。

了解一下nest的设计模式控制反转(Inversion of Control):

具体定义是高层模块不应该依赖低层模块,二者都应该依赖其抽象;抽象不应该依赖细节;细节应该依赖抽象。

class A {
    name: string
    constructor(name: string) {
        this.name = name
    }
}
 
 
class B {
    age:number
    entity:A
    constructor (age:number) {
        this.age = age;
        this.entity = new A('小满')
    }
}
 
const c = new B(18)
 
c.entity.name  //小满

可以看到,B 中代码的实现是需要依赖 A 的,两者的代码耦合度非常高。当两者之间的业务逻辑复杂程度增加的情况下,维护成本与代码可读性都会随着增加,并且很难再多引入额外的模块进行功能拓展。

为了解决这个问题可以使用IOC容器。

class A {
    name: string
    constructor(name: string) {
        this.name = name
    }
}
 
 
class C {
    name: string
    constructor(name: string) {
        this.name = name
    }
}
//中间件用于解耦
class Container {
    modeuls: any
    constructor() {
        this.modeuls = {}
    }
    provide(key: string, modeuls: any) {
        this.modeuls[key] = modeuls
    }
    get(key) {
        return this.modeuls[key]
    }
}
 
const mo = new Container()
mo.provide('a', new A('小满1'))
mo.provide('c', new C('小满2'))
 
class B {
    a: any
    c: any
    constructor(container: Container) {
        this.a = container.get('a')
        this.c = container.get('c')
    }
}
 
new B(mo)

其实就是写了一个中间件,来收集依赖,主要是为了解耦,减少维护成本。

三. 前置知识 装饰器

什么是装饰器

装饰器是一种特殊的类型声明,他可以附加在类,方法,属性,参数上面

1. 类装饰器

主要是通过@符号添加装饰器 他会自动把class的构造函数传入到装饰器的第一个参数 target

然后通过prototype可以自定义添加属性和方法

function decotators (target:any) {
    target.prototype.name = '小满'
}
 
@decotators
 
class Xiaoman {
 
    constructor () {
 
    }
 
}
 
const xiaoman:any = new Xiaoman()
 
console.log(xiaoman.name)

2.属性装饰器

同样使用@符号给属性添加装饰器

他会返回两个参数

1.原形对象

2.属性的名称

const currency: PropertyDecorator = (target: any, key: string | symbol) => {
    console.log(target, key)
}
 
 
class Xiaoman {
    @currency
    public name: string
    constructor() {
        this.name = ''
    }
    getName() {
        return this.name
    }
}

3. 参数装饰器

同样使用@符号给属性添加装饰器

他会返回两个参数

1.原形对象

2.方法的名称

3.参数的位置从0开始

const currency: ParameterDecorator = (target: any, key: string | symbol,index:number) => {
    console.log(target, key,index)
}
 
 
class Xiaoman {
    public name: string
    constructor() {
        this.name = ''
    }
    getName(name:string,@currency age:number) {
        return this.name
    }
}

4. 方法装饰器

同样使用@符号给属性添加装饰器

他会返回两个参数

1.原形对象

2.方法的名称

3.属性描述符 可写对应writable,可枚举对应enumerable,可配置对应configurable

const currency: MethodDecorator = (target: any, key: string | symbol,descriptor:any) => {
    console.log(target, key,descriptor)
}
 
 
class Xiaoman {
    public name: string
    constructor() {
        this.name = ''
    }
    @currency
    getName(name:string,age:number) {
        return this.name
    }
}