最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用NestJS開(kāi)發(fā)Node.js應(yīng)用的方法

 更新時(shí)間:2018年12月03日 10:48:38   作者:三毛丶  
這篇文章主要介紹了使用NestJS開(kāi)發(fā)Node.js應(yīng)用的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

NestJS 最早在 2017.1 月立項(xiàng),2017.5 發(fā)布第一個(gè)正式版本,它是一個(gè)基于 Express,使用 TypeScript 開(kāi)發(fā)的后端框架。設(shè)計(jì)之初,主要用來(lái)解決開(kāi)發(fā) Node.js 應(yīng)用時(shí)的架構(gòu)問(wèn)題,靈感來(lái)源于 Angular。在本文中,我將粗略介紹 NestJS 中的一些亮點(diǎn)。

組件容器

NestJS 采用組件容器的方式,每個(gè)組件與其他組件解耦,當(dāng)一個(gè)組件依賴于另一組件時(shí),需要指定節(jié)點(diǎn)的依賴關(guān)系才能使用:

import { Module } from '@nestjs/common';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';
import { OtherModule } from '../OtherModule';

@Module({
 imports: [OtherModule],
 controllers: [CatsController],
 providers: [CatsService],
})
export class CatsModule {}

依賴注入(DI)

與 Angular 相似,同是使用依賴注入的設(shè)計(jì)模式開(kāi)發(fā)

當(dāng)使用某個(gè)對(duì)象時(shí),DI 容器已經(jīng)幫你創(chuàng)建,無(wú)需手動(dòng)實(shí)例化,來(lái)達(dá)到解耦目的:

// 創(chuàng)建一個(gè)服務(wù)
@Inject()
export class TestService {
 public find() {
 return 'hello world';
 }
}

// 創(chuàng)建一個(gè) controller
@Controller()
export class TestController {
 controller(
 private readonly testService: TestService
 ) {}
 
 @Get()
 public findInfo() {
 return this.testService.find()
 }
}

為了能讓 TestController 使用 TestService 服務(wù),只需要在創(chuàng)建 module 時(shí),作為 provider 寫入即可:

@Module({
 controllers: [TestController],
 providers: [TestService],
})
export class TestModule {}

當(dāng)然,你可以把任意一個(gè)帶 @Inject() 的類,注入到 module 中,供此 module 的 Controller 或者 Service 使用。

背后的實(shí)現(xiàn)基于 Decorator + Reflect Metadata,詳情可以查看深入理解 TypeScript - Reflect Metadata 。

細(xì)粒化的 Middleware

在使用 Express 時(shí),我們會(huì)使用各種各樣的中間件,譬如日志服務(wù)、超時(shí)攔截,權(quán)限驗(yàn)證等。在 NestJS 中,Middleware 功能被劃分為 Middleware、Filters、Pipes、Grards、Interceptors。

例如使用 Filters,來(lái)捕獲處理應(yīng)用中拋出的錯(cuò)誤:

@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
 catch(exception: any, host: ArgumentsHost) {
 const ctx = host.switchToHttp();
 const response = ctx.getResponse();
 const request = ctx.getRequest();
 const status = exception.getStatus();

 // 一些其他做的事情,如使用日志

 response
  .status(status)
  .json({
  statusCode: status,
  timestamp: new Date().toISOString(),
  path: request.url,
  });
 }
}

使用 interceptor,攔截 response 數(shù)據(jù),使得返回?cái)?shù)據(jù)格式是 { data: T } 的形式:

import { Injectable, NestInterceptor, ExecutionContext } from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

export interface Response<T> {
 data: T;
}

@Injectable()
export class TransformInterceptor<T>
 implements NestInterceptor<T, Response<T>> {
 intercept(
 context: ExecutionContext,
 call$: Observable<T>,
 ): Observable<Response<T>> {
 return call$.pipe(map(data => ({ data })));
 }
}

使用 Guards,當(dāng)不具有 'admin' 角色時(shí),返回 401:

import { ReflectMetadata } from '@nestjs/common';
export const Roles = (...roles: string[]) => ReflectMetadata('roles', roles);

@Post()
@Roles('admin')
async create(@Body() createCatDto: CreateCatDto) {
 this.catsService.create(createCatDto);
}

數(shù)據(jù)驗(yàn)證

得益于class-validatorclass-transformer 對(duì)傳入?yún)?shù)的驗(yàn)證變的非常簡(jiǎn)單:

// 創(chuàng)建 Dto
export class ContentDto {
 @IsString()
 text: string
}

@Controller()
export class TestController {
 controller(
 private readonly testService: TestService
 ) {}
 
 @Get()
 public findInfo(
 @Param() param: ContentDto  // 使用
 ) {
 return this.testService.find()
 }
}

當(dāng)所傳入?yún)?shù) text 不是 string 時(shí),會(huì)出現(xiàn) 400 的錯(cuò)誤。

GraphQL

GraphQL 由 facebook 開(kāi)發(fā),被認(rèn)為是革命性的 API 工具,因?yàn)樗梢宰尶蛻舳嗽谡?qǐng)求中指定希望得到的數(shù)據(jù),而不像傳統(tǒng)的 REST 那樣只能在后端預(yù)定義。

NestJS 對(duì) Apollo server 進(jìn)行了一層包裝,使得能在 NestJS 中更方便使用。

在 Express 中使用 Apollo server 時(shí):

const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
 type Query {
 hello: String
 }
`;

// Provide resolver functions for your schema fields
const resolvers = {
 Query: {
 hello: () => 'Hello world!',
 },
};

const server = new ApolloServer({ typeDefs, resolvers });

const app = express();
server.applyMiddleware({ app });

const port = 4000;

app.listen({ port }, () =>
 console.log(`Server ready at http://localhost:${port}${server.graphqlPath}`),
);

在 NestJS 中使用它:

// test.graphql
type Query {
 hello: string;
}


// test.resolver.ts
@Resolver()
export class {
 @Query()
 public hello() {
 return 'Hello wolrd';
 }
}

使用 Decorator 的方式,看起來(lái)也更 TypeScript 。

其他

除上述一些列舉外,NestJS 實(shí)現(xiàn)微服務(wù)開(kāi)發(fā)、配合 TypeORM 、以及 Prisma 等特點(diǎn),在這里就不展開(kāi)了。

參考

深入理解 TypeScript - Reflect Metadata

Egg VS NestJS

NestJS 官網(wǎng)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • node.js+postman+mongodb搭建測(cè)試注冊(cè)接口的實(shí)現(xiàn)

    node.js+postman+mongodb搭建測(cè)試注冊(cè)接口的實(shí)現(xiàn)

    本文主要介紹了node.js+postman+mongodb搭建測(cè)試注冊(cè)接口的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • nodejs文件操作模塊FS(File System)常用函數(shù)簡(jiǎn)明總結(jié)

    nodejs文件操作模塊FS(File System)常用函數(shù)簡(jiǎn)明總結(jié)

    這篇文章主要介紹了nodejs文件操作模塊FS(File System)常用函數(shù)簡(jiǎn)明總結(jié),對(duì)FS模塊的大部份異步函數(shù)做了介紹,而且用中文注釋,這下用起來(lái)方便了,需要的朋友可以參考下
    2014-06-06
  • 修改Nodejs內(nèi)置的npm默認(rèn)配置路徑方法

    修改Nodejs內(nèi)置的npm默認(rèn)配置路徑方法

    今天小編就為大家分享一篇修改Nodejs內(nèi)置的npm默認(rèn)配置路徑方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • Express 框架中使用 EJS 模板引擎并結(jié)合 silly-datetime 庫(kù)進(jìn)行日期格式化的實(shí)現(xiàn)方法

    Express 框架中使用 EJS 模板引擎并結(jié)合 silly-datetime

    這篇文章主要介紹了Express 框架中使用 EJS 模板引擎并結(jié)合 silly-datetime 庫(kù)進(jìn)行日期格式化的實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式分析了express框架引入EJS模版以及導(dǎo)入 silly-datetime 庫(kù)的格式化方法傳遞給EJS模版使用的相關(guān)操作技巧,需要的朋友可以參考下
    2023-05-05
  • nodejs 生成和導(dǎo)出 word的實(shí)例代碼

    nodejs 生成和導(dǎo)出 word的實(shí)例代碼

    前段時(shí)間由于項(xiàng)目需求,得做excel和word的導(dǎo)出功能.這篇文章主要介紹了nodejs 生成和導(dǎo)出 word的實(shí)例代碼,需要的朋友可以參考下
    2018-07-07
  • nodejs中Express與Koa2對(duì)比分析

    nodejs中Express與Koa2對(duì)比分析

    提到Node.js開(kāi)發(fā),不得不提目前炙手可熱的2大框架express和koa。Express誕生已有時(shí)日,是一個(gè)簡(jiǎn)潔而靈活的web開(kāi)發(fā)框架,使用簡(jiǎn)單而功能強(qiáng)大。Koa相對(duì)更為年輕,是Express框架原班人馬基于ES6新特性重新開(kāi)發(fā)的敏捷開(kāi)發(fā)框架,現(xiàn)在可謂風(fēng)頭正勁,大有趕超Express之勢(shì)。
    2018-02-02
  • NodeJS學(xué)習(xí)筆記之Http模塊

    NodeJS學(xué)習(xí)筆記之Http模塊

    這里只是熟悉nodejs中的http模塊的API,一般在開(kāi)發(fā)過(guò)程中使用的是第三方的框架,比如說(shuō)Express。其中封裝了更為簡(jiǎn)單的構(gòu)建http服務(wù)器的API。
    2015-01-01
  • Node 模塊原理與用法詳解

    Node 模塊原理與用法詳解

    這篇文章主要介紹了Node 模塊原理與用法,結(jié)合實(shí)例形式詳細(xì)分析了node.js模塊基本概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下
    2020-05-05
  • 淺析 NodeJs 的幾種文件路徑

    淺析 NodeJs 的幾種文件路徑

    本篇文章主要介紹了淺析 NodeJs 的幾種文件路徑,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • nodemailer郵箱發(fā)送驗(yàn)證碼的實(shí)現(xiàn)

    nodemailer郵箱發(fā)送驗(yàn)證碼的實(shí)現(xiàn)

    郵箱注冊(cè)是常見(jiàn)的功能,通常需要發(fā)送郵箱驗(yàn)證碼驗(yàn)證,本文就來(lái)介紹一下nodemailer郵箱發(fā)送驗(yàn)證碼的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10

最新評(píng)論

碌曲县| 广灵县| 亳州市| 安吉县| 长沙县| 印江| 新野县| 长阳| 上林县| 元谋县| 文安县| 郁南县| 渝中区| 改则县| 横山县| 同德县| 类乌齐县| 渭源县| 达日县| 济宁市| 汨罗市| 汝城县| 安徽省| 鄂尔多斯市| 大同县| 确山县| 江都市| 任丘市| 阿瓦提县| 永和县| 那曲县| 永川市| 西藏| 都兰县| 黑河市| 克拉玛依市| 娄底市| 浪卡子县| 乌拉特中旗| 衡阳市| 武强县|