【问题标题】:nestjs configure https.agentnestjs 配置 https.agent
【发布时间】:2021-05-24 13:43:34
【问题描述】:

您好,我尝试使用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);
          });
      }
    
      }

获取用户信息

function is trigger in console 出现这个错误:

'无法读取未定义的属性'Agent''

它是在线参考:

 this.httpsAgent = new https.Agent({

我试图在互联网上搜索一些信息,但我没有找到任何东西,我们知道如何解决它吗?

【问题讨论】:

标签: https nestjs


【解决方案1】:

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

import { Request } from 'express';

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

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

【讨论】:

    【解决方案2】:

    试试import * as https from 'https'

    您也可以查看esModuleInterop 文档

    【讨论】:

      猜你喜欢
      • 2019-10-31
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      • 2020-10-31
      • 2019-06-19
      • 2019-09-18
      • 2021-11-26
      • 2020-09-05
      相关资源
      最近更新 更多