nestjs 發起請求 axios

1、下載

npm i --save @nestjs/axios axios

2、全局配置

import { HttpModule } from '@nestjs/axios';@Global()
@Module({imports: [HttpModule.registerAsync({inject: [ConfigService],useFactory: async (configService: ConfigService) => {return {timeout: configService.get('http.timeout'),maxRedirects: configService.get('http.maxRedirects'),};},}),],exports: [HttpModule],
})

全部全局模塊如下

import { Global, Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { HttpModule } from '@nestjs/axios';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import configuration from '../../config/index';
import { JwtModule } from '@nestjs/jwt';
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
import { JwtGuard } from 'src/utils/jwt/jwt-guard';
import { JwtStrategy } from 'src/utils/jwt/jwt-strategy';
import { WinstonService } from 'src/utils/logger/winston-service';
import { CatchLoggerFilter } from 'src/utils/logger/catch-logger-filter';
import { ResponseLoggerInterceptor } from 'src/utils/logger/response-logger-interceptor';
import { RedisModule } from '@nestjs-modules/ioredis';
import { RequirePermissionGuard } from 'src/utils/premission/require-premission.guard';
@Global()
@Module({imports: [ConfigModule.forRoot({isGlobal: true,load: [configuration],}),TypeOrmModule.forRootAsync({name:"default",inject: [ConfigService],useFactory: (configService: ConfigService) => {return {type: 'mysql',...configService.get('db.mysql'),timezone: '+08:00',// logger: 'advanced-console',entities: [__dirname + '/../**/*.entity.{js,ts}'],} as TypeOrmModuleOptions;},}),TypeOrmModule.forRootAsync({name: "oracle",inject: [ConfigService],useFactory: async (configService: ConfigService) => {return {type: 'oracle',...configService.get('db.oracle'),// logger: 'advanced-console',timezone: '+08:00',entities: [__dirname + '/../**/*.entity.{js,ts}'],} as TypeOrmModuleOptions;},}),HttpModule.registerAsync({inject: [ConfigService],useFactory: async (configService: ConfigService) => {return {timeout: configService.get('http.timeout'),maxRedirects: configService.get('http.maxRedirects'),};},}),RedisModule.forRootAsync({inject: [ConfigService],useFactory: (configService: ConfigService) => {return {type: "single",url: configService.get('redis.url'),};},}),JwtModule.registerAsync({inject: [ConfigService],global: true,useFactory: (configService: ConfigService) => {return {secret: configService.get('jwt.secretkey'),// signOptions: { expiresIn: configService.get('jwt.expiresin') },};},})],providers: [JwtStrategy,{provide: APP_GUARD,useFactory: (configService: ConfigService) => {return new JwtGuard(configService);},inject: [ConfigService],},{provide: APP_GUARD,useClass: RequirePermissionGuard},{provide: WinstonService,inject: [ConfigService],useFactory: (configService: ConfigService) => {return new WinstonService(configService);}},{provide: APP_FILTER,useClass: CatchLoggerFilter},{provide: APP_INTERCEPTOR,useClass: ResponseLoggerInterceptor}],exports: [WinstonService,HttpModule],
})
export class ShareModule { }

3、使用

import { Injectable } from "@nestjs/common";
import { HttpService } from "@nestjs/axios";
import { firstValueFrom } from "rxjs";@Injectable()
export class TestService {constructor(private readonly httpService: HttpService,) { }async getTest() {const observable = this.httpService.get('https://api.github.com/users/yangxiaodong');// 請求成功后使用firstValueFrom方法獲取結果const response = await firstValueFrom(observable);return response.data}
}
# 開發環境配置
env: 'development'
app:prefix: 'api'port: 8080logger:# 項目日志存儲路徑,相對路徑(相對本項目根目錄)或絕對路徑dir: '../logs'# 文件相關file:# 是否為本地文件服務或cosisLocal: true# location 文件上傳后存儲目錄,相對路徑(相對本項目根目錄)或絕對路徑location: '../upload'# 文件服務器地址,這是開發環境的配置 生產環境請自行配置成可訪問域名domain: 'http://localhost:8080'# 文件虛擬路徑, 必須以 / 開頭, 如 http://localhost:8080/profile/****.jpg  , 如果不需要則 設置 ''serveRoot: '/profile'# 文件大小限制,單位MmaxSize: 10
# 騰訊云cos配置
cos:secretId: ''secretKey: ''bucket: ''region: ''domain: ''location: ''
# 數據庫配置
db:mysql:host: '127.0.0.1'username: 'root'password: '123456789'database: 'nestjs'port: 3306charset: 'utf8mb4'logger: 'file'logging: truemultipleStatements: truedropSchema: falsesynchronize: falsesupportBigNumbers: truebigNumberStrings: trueoracle:host: '192.168.20.171'port: 1521username: 'flyco_md'password: 'password'serviceName: 'maindb'synchronize: false# redis 配置
redis:url: 'redis://localhost:6379'password: 123456# jwt 配置
jwt:secretkey: 'you_secretkey'expiresin: '9999y'# axios配置
http:timeout: 5000maxRedirects: 5
# 權限 白名單配置
perm:router:whitelist:[{ path: '/api/auth/getCaptCha', method: 'GET' },{ path: '/api/auth/login', method: 'POST' },{ path: '/register', method: 'POST' },{ path: '/api/test/test', method: 'GET' },{ path: '/logout', method: 'POST' },{ path: '/perm/{id}', method: 'GET' },{ path: '/upload', method: 'POST' },]# 是否開啟驗證碼
sys:captchaEnabled: true

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/95481.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/95481.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/95481.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

將 Logits 得分轉換為概率,如何計算

場景:動物識別,輸入一張28*28的圖像,模型輸出屬于 貓、狗、鳥 哪個類型。需求:假設模型 ??Logits(模型在每個類別的置信度得分) 輸出為??:[貓: 3.2, 狗: 1.5, 鳥: -0.8]。計算 ??Softmax …

【Qt】bug排查筆記——QMetaObject::invokeMethod: No such method

問題如題目所示:QMetaObject::invokeMethod: No such method xxxx,在網上好一頓查,又將查到的資料喂給了 Ai,才最終將問題解決,特此記錄下。 一、問題背景 在做公司項目時,使用了插件的方式開發。主程序加載…

Spring Boot手寫10萬敏感詞檢查程序

使用Spring Boot手寫10萬敏感詞檢查程序 本文將介紹如何使用Spring Boot構建一個高效的敏感詞檢查系統,能夠處理多達10萬個敏感詞的檢測需求。我們將使用DFA(Deterministic Finite Automaton)算法來實現高效匹配,并提供RESTful API接口。 實現步驟 1. 創建Spring Boot項…

零構建的快感!dagger.js 與 React Hooks 實現對比,誰更優雅?

“Add Tags” 技術方案并行對比:React Hooks vs dagger.js(含核心 JS 代碼) 源碼: React Hooks:https://codepen.io/prvnbist/pen/jJzROe?editors1010dagger.js:https://codepen.io/dagger8224/pen/ZErjzw…

矩池云中LLaMA- Factory多機多卡訓練

LLaMA Factory 是一款開源低代碼大模型微調框架,集成了業界最廣泛使用的微調技術,支持通過 Web UI 界面零代碼微調大模型,目前已經成為開源社區內最受歡迎的微調框架之一。但是在矩池云上如何使用LLaMA-Factory多機多卡訓練模型呢&#xff1f…

Nginx的反向代理與正向代理及其location的配置說明

一、Nginx中location匹配優先級Nginx中location匹配優先級location支持各種匹配規則,在多個匹配規則下,Nginx對location的處理是有優先級的,優先級高的規則會優先進行處理;而優先級低的規則可能會最后處理或者不進行處理。注意&am…

神經網絡正則化三重奏:Weight Decay, Dropout, 和LayerNorm

正則化是機器學習中防止模型過擬合、提升泛化能力的核心技術。Weight Decay、Dropout和LayerNorm是三種最常用的方法,但它們的工作原理和首要目標截然不同。下面的流程圖揭示了它們的核心區別與聯系: #mermaid-svg-vymek6mFvvfxcWiM {font-family:"…

兩臺電腦通過網線直連共享數據,設置正確,卻互相ping不通的解決方法

因為某些原因,需要兩臺電腦互傳資源,但是某臺電腦可能無法連接外網。如果手頭有根網線,很容易想到通過一根網線連接兩臺電腦互傳數據。 這里先說一下基本的設置: 兩臺電腦最好都關閉防火墻;兩臺電腦都打開專用網絡和公…

面試新紀元:無聲勝有聲,讓AI成為你頸上的智慧伙伴

面試,無論是對于面試官還是求職者,都像一場無聲的戰爭。 一方要精準識人,一方要完美自薦;一方怕問不到點子上,一方怕答不到心坎里。 緊張、遺忘、表達失誤、準備不足……這些問題幾乎每個人都經歷過。 有沒有一種方…

qt-C++筆記之QtDesigner-Creator按鈕圖標與樣式

qt-C筆記之QtDesigner-Creator按鈕圖標與樣式 整理:如何用 .qrc 管理資源、在 Designer/Creator 中為 QPushButton 設置圖標(資源或系統主題),以及用樣式表調整文字樣式。涵蓋 C/Qt 與 PySide/PyQt;Linux 桌面優先&am…

maven 常用指令

Maven 是 Java 項目構建和依賴管理的得力助手。這里為你總結了一些常用指令,希望能幫你提升開發效率。下面這個表格匯總了 Maven 最核心和常用的一些命令:命令主要功能典型使用場景mvn clean清理項目,刪除 target 目錄及其所有編譯輸出文件。…

# pdf.js完全指南:構建現代Web PDF查看與解析解決方案

在當今Web開發中,實現高質量的PDF查看功能一直是前端開發者面臨的挑戰之一。作為最受歡迎的JavaScript PDF庫,pdf.js已經成為解決這一問題的行業標準。由Mozilla開發并維護的pdf.js項目,通過純JavaScript實現PDF解析與渲染,徹底改…

高效對象屬性復制工具

日常編程中,經常會碰到對象屬性復制的場景,比如 VO、DTO、PO、VO 等之間的轉換,關于什么是VO、DTO、PO、VO 等可以看上篇文章,VO、DTO、PO、VO 等對象具體有哪些方式可以使用呢? set/get 方式 性能最好的方式&#x…

大疆圖傳技術參數對比 你了解多少?

無人機是現代航空技術與智能控制技術結合的產物,已從軍事領域廣泛滲透至民用場景,成為推動各行業效率升級的關鍵工具。無人機的全稱為 “無人駕駛航空器(Unmanned Aerial Vehicle,簡稱 UAV)”,簡言之&#…

Redis 緩存熱身(Cache Warm-up):原理、方案與實踐

在 Redis 緩存架構中,“緩存熱身”是指在系統正式提供服務前(如重啟、擴容后),主動將熱點數據加載到 Redis 中的操作。其核心目標是避免**緩存穿透**(請求直達數據庫)和**緩存雪崩**(大量請求同…

基于SpringBoot的大學生就業招聘系統

1. 在線演示: 后臺:http://springbootiv1oo.xiaobias.com/springbootiv1oo/admin/dist/index.html 前臺:http://springbootiv1oo.xiaobias.com/springbootiv1oo/front/index.html 管理員:abo/abo 用戶:用戶1/123456、…

Java反序列化漏洞揭秘:從原理到攻擊實戰

一、背景 熟悉接口開發的同學一定知道,能將數據對象很輕松的實現多平臺之間的通信、對象持久化存儲,序列化和反序列化是一種非常有效的手段,例如如下應用場景,對象必須 100% 實現序列化。 DUBBO:對象傳輸必須要實現序…

Time-MOE 音頻序列分類任務

prompt 我準備做語音疾病分類任務。語音音頻是 WAV 格式的音頻,基本上分為兩類,分別是疾病類和非疾病類。也有少數數據集是多分類,現在我找到了26個數據集,我準備我已經在 MLP CNN 上面測試了它們的基準,下面我找到了一…

[嵌入式embed][Qt]Qt5.12+Opencv4.x+Cmake4.x_測試Qt編譯的opencv4.x的庫

[嵌入式embed][Qt]Qt5.12Opencv4.xCmake4.x_測試Qt編譯的opencv4.x的庫編譯Qt-Opencv庫測試流程-①創建一個簡單的qt-ui工程配置 & 測試配置庫編譯環境測試代碼百度云-工程(opencv4.xqt5.12的工程)參考文檔編譯Qt-Opencv庫 [嵌入式embed][Qt]Qt5.12Opencv4.xCmake4.x_用Qt…

相較于傳統AR礦物鑒定有哪些優勢?

與傳統的礦物鑒定方法相比,AR礦物鑒定就像是一位全面升級的“超級助手”,展現出了無可比擬的優勢。傳統的礦物鑒定方法,往往依賴于地質學家或專業鑒定人員的豐富經驗。他們需要通過肉眼觀察礦物的顏色、光澤、硬度等物理特征,再結…