嗨我尝试使用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);
});
}
}
当
获取用户信息
功能在控制台中触发,出现此错误:
'无法读取未定义的属性'Agent''
这是网上参考:
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);
}
尝试使用动态模块
创建一个稍后导入的变量,这是nestjs的建议。
export const HttpsAgentClient = 'HttpsAgentClient'
创建一个动态模块,它接受三个参数作为证书、密钥和 ca 的路径。
import { DynamicModule, Module, Provider } from '@nestjs/common';
import https from 'https';
import fs from 'fs';
import { HttpsAgentClient } from './constants';
@Module({})
export class HttpsAgentModule {
static forRoot(certPath: string, keyPath: string, caPath: string): DynamicModule {
const httpsAgentProvider: Provider = {
provide: HttpsAgentClient,
useFactory: () => {
return new https.Agent({
cert: fs.readFileSync(certPath, { encoding: 'utf8' }),
key: fs.readFileSync(keyPath, { encoding: 'utf8' }),
ca: fs.readFileSync(caPath, { encoding: 'utf8' }),
});
}
};
return {
module: HttpsAgentModule,
providers: [httpsAgentProvider],
exports: [httpsAgentProvider]
};
}
}
将动态模块导入到目标模块中。
import { Module } from '@nestjs/common';
import { ExampleController } from './example.controller';
import { ExampleService } from './example.service';
import { HttpsAgentModule } from './httpsAgent/httpsAgent.module';
@Module({
controllers: [ExampleController],
providers: [ExampleService],
imports: [
HttpsAgentModule.forRoot(
'./ssl/certPath.pem',
'./ssl/keyPath.key',
'./ssl/caPath.pem'
)
]
})
export class ExampleModule {}
将动态模块注入目标服务文件。
import { Inject, Injectable } from '@nestjs/common';
import { HttpsAgentClient } from './httpsAgent/constants';
@Injectable()
export class ExampleService {
constructor(
@Inject(HttpsAgentClient)
private readonly httpsAgent: object,
) {}
}
如果合适,可以在模块导入中定义选项
rejectUnauthorized
:
import { Module } from '@nestjs/common';
import { HttpModule } from '@nestjs/axios';
import * as https from 'https';
@Module({
imports: [
HttpModule.register({
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
}),
],
// ...
})
export class MyModule {}