问题
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