未使用node-fetch调用的快速路由

我有一台路由的快递服务器。在浏览器中调用此路由时,我可以获取服务器发送的数据。但是,当我尝试通过简单的node-fetch从脚本调用此路由时,我有一个响应,但未调用我的路由(未检索到数据)。我解释 :

这是我的快递服务器的代码:

应用程式

import express from "express";
import httpServer from "./httpServer";
import HttpRoutes from "./httpRoutes";

class BrokerServer {
  public httpServer!: express.Application;
  public httpRoutes!: HttpRoutes;

  constructor() {
    this.initHttpServer();
  }

  private initHttpServer(): void {
    this.httpServer = httpServer;
    this.httpRoutes = new HttpRoutes();
    this.httpRoutes.routes();
  }
}

new BrokerServer();

服务器

import express from "express";
import * as bodyParser from "body-parser";

class HttpServer {
  public HTTP_PORT: number = 9000;
  public server!: express.Application;

  constructor() {
    this.server = express();
    this.server.use(bodyParser.json());

    this.server.listen(this.HTTP_PORT, () => {
      console.log('Broker HTTP Server listening on port 9000');
    });
  }
}

export default new HttpServer().server;

还有我的routes.ts

import httpServer from "./httpServer";

export default class HttpRoutes {
  public routes(): void {
    httpServer.get("/getNodes", (req, res) => {
      console.log("GET");
      res.status(200).send(JSON.stringify({ nodes: [] }));
    });
  }
}

When I launch my server and naviguate on the url http://localhost:9000/getNodes I can see my console.log('GET'). this is not the case when I try with node-fetch.

这是我的小脚本:

const fetch = require('node-fetch');

console.log('launch fetch');

fetch('http://localhost:9000/getNodes')
.then(response => {
    console.log('response', response);
    return response.json()
})
.then(results => {
    console.log('results', results);
})
.catch(error => {
    console.log('error', error);
});

With the script I reach the console.log('response', response); but never my results.

有人知道问题出在哪里吗?

评论