douduan3203 2019-02-08 00:19
浏览 343

通过cURL与NodeJS Express URL的连接失败

I have a VPS hosted with OVH for a NodeJS script. The VPS runs an Express NodeJS script on port 3000 to return a JSON. I have a web server hosted with GoDaddy for a Website. The the web server hosts my website.

I require the website to access the NodeJS script and retrieve the JSON data that is returned.

I KNOW that the NodeJS script works as I can enter the URL into my web browser and the JSON content is returned and displayed in-browser. I have ALSO hosted my website locally and the site works perfectly. The PHP script successfully retrieves the NodeJS JSON data and no errors occur.

HOWEVER when I upload my website to the GoDaddy web server; my PHP script fails to connect to the OVH VPS and when dumping curl_error($ch) i get the error: Failed to connect to {vps ip} port 3000: Connection refused.

I've tried altering the URLs prefix "https://", "http://". I've tried PHP's file_get_contents() with a similar issue arising.

The Express server runs off of:

app.use(cors());
app.get("/server/:id", async (req, res, next) => {
    let server = req.params.id;
    ...
    return res.json({ "foo": "bar" });
});

app.listen(3000, () => {
    console.log(`>> API Connecting          >> 3000`);
});

My PHP script attempts to access the above through:

$ch = curl_init("http://{OVH VPS IP}/server/12345678");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result, true);

Id expect $response to be an array of the returned $result however actually $result == false and $response == NULL

Any assistance is GREATLY appreciated!

  • 写回答

1条回答 默认 最新

  • dougong2306 2019-02-15 02:26
    关注

    Try specify second argument of below function (the IP address) like so:

    app.listen(3000, '0.0.0.0', () => {
        console.log(`>> API Connecting          >> 3000`);
    });
    

    '0.0.0.0' is listen on all interfaces,

    '127.0.0.1' is probably default value, when You don't specify second argument

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?