nestjs配置https.agent [英] nestjs configure https.agent

查看:36
本文介绍了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);
          });
      }
    
      }

getUserInformation

功能是控制台中的触发器出现此错误:

‘无法读取未定义’的属性‘Agent’

在线引用:

 this.httpsAgent = new https.Agent({

我尝试在Internet上搜索一些信息,但什么也没有找到,我们知道如何解决它吗?

推荐答案

从Express获取用户代理导入请求

import { Request } from 'express';

然后使用Request在您的控制器/服务/中间件中使用它。 此处给出了控制器示例。

@Get()
someMethodName(@Req() request: Request) {
   const userAgent = request.get('user-agent');
   console.log(userAgent);
}

这篇关于nestjs配置https.agent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆