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矿物鉴定就像是一位全面升级的“超级助手”,展现出了无可比拟的优势。传统的矿物鉴定方法,往往依赖于地质学家或专业鉴定人员的丰富经验。他们需要通过肉眼观察矿物的颜色、光泽、硬度等物理特征,再结…