在 Nestjs 中使用 https 和 Axios 请求

And*_*rew 3 javascript https typescript axios nestjs

我目前有一个 Nestjs 服务器设置,并尝试在其中一个端点收到 GET 请求时执行 Axios 请求。这是controller.ts代码:

@Controller()
export class TestController {
    constructor(private readonly testService: TestService) {}

    @Get('testData')
    testData() {
        return this.testService.testData();
    }
}
Run Code Online (Sandbox Code Playgroud)

服务.ts:

@Injectable()
export class TestService {
    status(): string {
        return 'OK'
    }

    testData(): Promise<any> {
        return helper.getTestData();
    }
}
Run Code Online (Sandbox Code Playgroud)

其中helper.getTestData()只是使用以下函数调用辅助文件:

export async function getTestData(): Promise<any> {
    const result = await axios({
        url: tempURL,
        method: 'GET',
        timeout: 3000,
        httpsAgent: new https.Agent({
            rejectUnauthorized: false,
        }),
    });
Run Code Online (Sandbox Code Playgroud)

我能够到达此端点tempURL,但遇到以下错误消息:Cannot read property 'Agent' of undefined。我知道我尝试访问的端点需要证书,这就是为什么我必须在 Axios 请求中包含 httpsAgent 参数。如果我不包含参数httpsAgent,我会收到以下消息Error: unable to verify the first certificate in nodejs

有没有办法配置 Nestjs 以使用 https?或者是否有另一种方法可以在 Nestjs 内部处理此授权问题?使用 Postman 一切正常,所以我假设这是 Nestjs 问题。任何帮助表示赞赏。

Mic*_*evi 6

而不是import https from 'https';你应该使用命名空间 import :或在你的 tsconfig 文件中import * as https from 'https';设置esModuleInteropto (在 下)truecompilerOptions