https - nestjs 配置 https.agent
问题描述
您好我尝试使用nestJs 框架来构建一个简单的应用程序。此应用程序调用 http 服务来检索和记录一些信息。我附上nestjs服务的代码:
import { HttpService, Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { UserInformation } from 'src/models/userInformation';
import https from 'https';
@Injectable()
export class CallService {
private authRequest: any;
private httpsAgent: https.Agent;
constructor(
private configService: ConfigService,
private httpService: HttpService,
) {
this.httpsAgent = new https.Agent({
rejectUnauthorized: false,
});
this.authRequest = {
username: configService.get('username'),
password: configService.get('password'),
};
}
getUserInformation(): Promise<UserInformation> {
var url = this.configService.get('apiEndpoint') + 'scrapt/getUserInfo';
return this.httpService
.get(url, {
auth: this.authRequest,
httpsAgent: this.httpsAgent,
})
.toPromise()
.then((response) => {
return new UserInformation(response.data);
});
}
}
当。。。的时候
获取用户信息
功能在控制台中触发出现此错误:
'无法读取未定义的属性'代理''
它是在线参考:
this.httpsAgent = new https.Agent({
我试图在互联网上搜索一些信息,但我没有找到任何东西,我们知道如何解决它吗?
解决方案
从 express 获取用户代理导入请求
import { Request } from 'express';
然后在你的控制器/服务/中间件中使用它Request
。这里给出了控制器示例。
@Get()
someMethodName(@Req() request: Request) {
const userAgent = request.get('user-agent');
console.log(userAgent);
}
推荐阅读
- python - 将 tf1 中的代码转换为 tf2 时出错
- css - 在 Jupyterbooks 中为 Sphinx 指令(如警告)修改文本格式
- django - Django Query 选择乘法计数
- javascript - Flickity:未捕获的 TypeError:无法读取 null 的属性“querySelectorAll”
- flutter - Flutter:device_apps包圆圈图标问题
- c - 为什么我在函数 addRandomNumbers 中出现错误?
- javascript - 如何下载 React 组件的一部分?
- css - CSS:为外部超链接添加图标,但当图像是超链接时不添加
- linux - 无法在 M1 macbook 上安装 Chromium inisde docker 容器
- javascript - 即使依赖项一直没有改变,useEffect 钩子也会变成无限循环