本文介绍了Node.JS无法调用在其他端口上的本地主机上运行的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要执行的操作是我的应用程序在端口3030的localhost上运行我已经写了这段代码

What i am trying to do is that that my application is running on localhost on port 3030i 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);
    }
})

所以我在本地主机上调用另一个在端口3000上运行的应用程序服务(本地主机的默认IP为127.0.0.1).但是,此请求始终无法正常工作,并且错误输出错误:解析错误".我的端口3030的服务已成功调用,但在该服务中该请求从未被调用.任何帮助将不胜感激.

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.

这是此服务返回的杰森("/showdb")

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

结果:[{名称:"bridgevine",sizeOnDisk:486539264,空:错误},{名称:"crud",sizeOnDisk:218103808,空:错误},{名称:"exascaleDb",sizeOnDisk:486539264,空:错误},{名称:本地",sizeOnDisk:83886080,空:错误},{名称:"mydb",sizeOnDisk:218103808,空:错误},{名称:"test",sizeOnDisk:218103808,空:错误},{名称:"winedb",sizeOnDisk:218103808,空:错误}]

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 }]

推荐答案

我怀疑解析错误发生在以下任一行中:

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

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

注释一个或另一个.将主体包装在JSON.stringify(body)中.如果此问题得以解决,则意味着3000服务器正在发送JSON,但您的行为就像是文本.

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.

这篇关于Node.JS无法调用在其他端口上的本地主机上运行的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 19:51