我正试图用nest发出请求。js HttpService是在他们的文档中指定的,但我一直收到一个我无法理解的错误。
我已经导入了HttpModule
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [HttpModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
我正试图像这样使用HttpService:
import { HttpService } from '@nestjs/axios';
import { Controller, Get } from '@nestjs/common';
import { lastValueFrom } from 'rxjs';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(
private readonly appService: AppService,
private httpService: HttpService
) {}
@Get()
async getHello(): Promise<string> {
const req = this.httpService.get('https://icanhazdadjoke.com/');
console.log(req)
const res = await lastValueFrom(req);
const data = res.data
return data
}
}
但当我使用curl查询get端点时,会得到一个错误响应
http-test git:(master) â curl localhost:3001
{"statusCode":500,"message":"Internal server error"}%
我的控制台显示如下
[11:02:02 a.m.] File change detected. Starting incremental compilation...
[11:02:02 a.m.] Found 0 errors. Watching for file changes.
[Nest] 73798 - 2022-03-22, 11:02:03 a.m. LOG [NestFactory] Starting Nest application...
[Nest] 73798 - 2022-03-22, 11:02:03 a.m. LOG [InstanceLoader] HttpModule dependencies initialized +36ms
[Nest] 73798 - 2022-03-22, 11:02:03 a.m. LOG [InstanceLoader] AppModule dependencies initialized +1ms
[Nest] 73798 - 2022-03-22, 11:02:03 a.m. LOG [RoutesResolver] AppController {/}: +5ms
[Nest] 73798 - 2022-03-22, 11:02:03 a.m. LOG [RouterExplorer] Mapped {/, GET} route +2ms
[Nest] 73798 - 2022-03-22, 11:02:03 a.m. LOG [NestApplication] Nest application successfully started +2ms
Observable { _subscribe: [Function (anonymous)] }
[Nest] 73798 - 2022-03-22, 11:02:06 a.m. ERROR [ExceptionsHandler] Request failed with status code 503
Error: Request failed with status code 503
at createError (/Users/sv/projects/http-test/node_modules/@nestjs/axios/node_modules/axios/lib/core/createError.js:16:15)
at settle (/Users/sv/projects/http-test/node_modules/@nestjs/axios/node_modules/axios/lib/core/settle.js:17:12)
at IncomingMessage.handleStreamEnd (/Users/sv/projects/http-test/node_modules/@nestjs/axios/node_modules/axios/lib/adapters/http.js:322:11)
at IncomingMessage.emit (events.js:412:35)
at endReadableNT (internal/streams/readable.js:1317:12)
at processTicksAndRejections (internal/process/task_queues.js:82:21)