使用Express、Request和Node.js发送/处理typescript中的GET请求

使用Express、Request和Node.js发送/处理typescript中的GET请求,node.js,express,typescript,npm-request,Node.js,Express,Typescript,Npm Request,我将使用Express和Request(使用npm安装)的组合来尝试发送get请求,以从服务器获取一些json。然而,无论我做什么,返回的身体是“未定义的” 这是我的server.js文件中的代码。json实际上不是我要发送的内容,它只是一个示例,因为我无法发布我实际发送的内容 import express = require("express"); import bodyParser = require("body-parser"); let app = express(); app.use(

我将使用Express和Request(使用npm安装)的组合来尝试发送get请求,以从服务器获取一些json。然而,无论我做什么,返回的身体是“未定义的”

这是我的server.js文件中的代码。json实际上不是我要发送的内容,它只是一个示例,因为我无法发布我实际发送的内容

import express = require("express");
import bodyParser = require("body-parser");
let app = express();
app.use(bodyParser.json());

app.get('/config', function(req, res){
    res.json('{name: test}');
})

app.listen(3000);
我试过以下两种方法,但都说身体是未定义的

import request = require("request");

let req = {
    url: `http://localhost:3000/config`,
    method: 'GET',
    headers: {
        'Content-Type': 'application/json'
    }
}

request(req, function(error, response, body){
    this.config = JSON.parse(body);
})

request(`/config`, function(err, res, body) {
    this.config = JSON.parse(body);
});
有人知道我做错了什么吗?我以前从未使用过express或request,因此任何提示都将不胜感激


更新

如果我将请求代码更改为以下代码,则函数的内部永远不会运行。有人知道为什么会这样吗

let req = {
    url: `http://localhost:3000/config`,
    method: 'GET',
    headers: {
        'Content-Type': 'application/json'
    }
}

request(req, function(error, response, body){
    console.log("response => "+JSON.parse(body));
    return JSON.parse(body);
})

我不知道您是否发布了服务器的全部代码,您似乎错过了
app.listen(port)
,因此无法正确启动服务器

另外,如果您在
请求的回调函数的第一行添加了
if(error){console.log(error);}
,您会发现它打印了一个错误:
[错误:无效URI”/config]

这就是为什么
正文
总是
未定义的原因:您必须给出完整的url,如
http://localhost:xxxx
请求

简言之:

  • 您的服务器未侦听特定端口<代码>应用程序收听(5678)
  • 您的客户不知道完整的url<代码>请求('http://localhost:5678/config“,(…)=>{…})

    • 因为OP没有让它工作,我相信他在那里得到的代码是正确的。我不妨在这里发布我的工作解决方案,以帮助他开始工作

      希望这将节省您调试的时间

      客户端:

      "use strict";
      let request = require("request");
      
      let req = {
          url: `localhost:4444/config`,
          proxy: 'http://localhost:4444',
          method: 'GET',
          headers: {
              'Content-Type': 'application/json'
          }
      };
      
      request(req, function (err, res, body) {
          this.config = JSON.parse(body);
          console.log("response => " + this.config);
      });
      
      "use strict";
      var express = require("express");
      var bodyParser = require("body-parser");
      var app = express();
      var config = require('config');
      app.use(bodyParser.json());
      
      app.get('/config', function(req, res){
          res.json('{name: test}');
      });
      
      // Start the server
      app.set('port', 4444);
      
      app.listen(app.get('port'), "0.0.0.0", function() {
          console.log('started');
      });
      
      服务器:

      "use strict";
      let request = require("request");
      
      let req = {
          url: `localhost:4444/config`,
          proxy: 'http://localhost:4444',
          method: 'GET',
          headers: {
              'Content-Type': 'application/json'
          }
      };
      
      request(req, function (err, res, body) {
          this.config = JSON.parse(body);
          console.log("response => " + this.config);
      });
      
      "use strict";
      var express = require("express");
      var bodyParser = require("body-parser");
      var app = express();
      var config = require('config');
      app.use(bodyParser.json());
      
      app.get('/config', function(req, res){
          res.json('{name: test}');
      });
      
      // Start the server
      app.set('port', 4444);
      
      app.listen(app.get('port'), "0.0.0.0", function() {
          console.log('started');
      });
      
      输出:

      "use strict";
      let request = require("request");
      
      let req = {
          url: `localhost:4444/config`,
          proxy: 'http://localhost:4444',
          method: 'GET',
          headers: {
              'Content-Type': 'application/json'
          }
      };
      
      request(req, function (err, res, body) {
          this.config = JSON.parse(body);
          console.log("response => " + this.config);
      });
      
      "use strict";
      var express = require("express");
      var bodyParser = require("body-parser");
      var app = express();
      var config = require('config');
      app.use(bodyParser.json());
      
      app.get('/config', function(req, res){
          res.json('{name: test}');
      });
      
      // Start the server
      app.set('port', 4444);
      
      app.listen(app.get('port'), "0.0.0.0", function() {
          console.log('started');
      });
      
      响应=>{name:test}


      我复制粘贴了您的第一个示例代码,并使用
      postman
      返回了“{name:test}”。这就是你期望的结果吗?是的,这就是我想要的。我想我的请求一定是出了问题。我不确定。我只是在做一个简单的“获取”。空标题。也许值得一看您用来进行调用的工具或API,也许它在原始响应中添加了一些胡椒和盐。我还会检查HTTP响应代码,确保它没有得到404。在客户端代码中,您的url应该是
      localhost:3000/config
      。当我在postman中这样做时,它会说“无法得到/config”。您是如何得到响应的?它被设置为自动使用localhost。我也有app.listen(3000)到端口,我只是忘了把它包括在我的评论中。是否自动使用localhost?你是说你的客户要求吗?不会的,伊莫。而且您没有告诉客户端应该将请求发送到哪个端口。只要打印一个关于错误的日志,你就会知道它不起作用的原因。错误消息总是非常有用的。@annedroiid告诉我这是否对您有效。注意,我在这里使用的是不同的端口<代码>4444当我将其放入时,我得到“错误:无效协议:localhost:@annedroiid无法复制它…”。。。我会删除代理行,看看会发生什么。。。