axios 或 @nestjs/axios 错误地应用 npm config proxy

npm proxy

$ npm config get proxy
http://127.0.0.1:25378/

如果你的npm设置了如上代理,那么当请求本地服务时,应该会出现代理错误(通常是502响应):

import { HttpService } from '@nestjs/axios';

@Injectable()
export class ForwardService {
  @Inject()
  private readonly http: HttpService;
  ...
  await this.http.request(...).toPromise(); // AxiosError: 502
}

解决办法

设置 no_proxy 环境变量:

"start:dev": "set no_proxy=127.0.0.1 && set NODE_ENV=dev&&nest start --watch"

see: https://github.com/axios/axios/issues/2160

posted @ 2023-05-29 15:42  develon  阅读(141)  评论(0编辑  收藏  举报