小编And*_*rew的帖子

在 Nestjs 中使用 https 和 Axios 请求

我目前有一个 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。我知道我尝试访问的端点需要证书,这就是为什么我必须在 …

javascript https typescript axios nestjs

3
推荐指数
1
解决办法
4204
查看次数

标签 统计

axios ×1

https ×1

javascript ×1

nestjs ×1

typescript ×1