Node.JS Unable to call a service running on localhost on different port

99封情书 提交于 2020-01-16 14:08:13

问题


What i am trying to do is that that my application is running on localhost on port 3030 i have written this code

var request = require('request');

request("http://127.0.0.1:3000/showdb", function (error, response, body) {
    if(error)
        console.log("ERROR IS :"+error);

    if (!error && response.statusCode == 200) {
        console.log("body"+body) // Print the google web page.
        res.send(body);
    }
})

so i am calling another service of application running on port 3000 on my localhost.(127.0.0.1 default ip for localhost). But its not working this request is never made and error outputs "Error: Parse Error". My service of port 3030 is successfully called but in that service that request is never called. Any help would be greatly appreciated.

Here is the jason returned by this service ("/showdb")

result: [ { name: "bridgevine", sizeOnDisk: 486539264, empty: false }, { name: "crud", sizeOnDisk: 218103808, empty: false }, { name: "exascaleDb", sizeOnDisk: 486539264, empty: false }, { name: "local", sizeOnDisk: 83886080, empty: false }, { name: "mydb", sizeOnDisk: 218103808, empty: false }, { name: "test", sizeOnDisk: 218103808, empty: false }, { name: "winedb", sizeOnDisk: 218103808, empty: false } ]


回答1:


I'd suspect the parse error happens in either of these lines:

    console.log("body"+body) // Print the google web page.
    res.send(body);

Comment one or the other out. Wrap body in JSON.stringify(body). If this fixes, it means the 3000 server is sending JSON but you are acting like it is text.



来源:https://stackoverflow.com/questions/18586545/node-js-unable-to-call-a-service-running-on-localhost-on-different-port

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!