Soull
Soull

Reputation: 37

Failed connection over cURL to 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!

Upvotes: 2

Views: 1188

Answers (1)

Vraith
Vraith

Reputation: 11

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

Upvotes: 1

Related Questions