0

I have been working on a NodeJS express app that uses EJS and node-fetch to fetch my Api from my vps server, but it won't get fetched, every page of the app will load but the page that uses the Api where I am fetching the Api won't work, I have been trying to solve this issue for almost a week now but cannot get anywhere

My App.js

const express = require('express');

//node fetch used here is a fork of the original and i have tried both original and this
//both have the same result

const fetch = require('@formio/node-fetch-http-proxy');
const port = 3000;

...
...
... <- some code here
...
...

app.get('/', (req, res) => {
    res.render('index');
});

app.get('/xyz',(req,res) => {
    var url = 'http://XX.XX.XX.XX:8080/api';

    try {
        fetch(url, {
            method: 'GET',
            headers: {
                'api-key': process.env.API_KEY,
                'Content-Type': 'application/json'
            }
        }).then((resp) => {
            return resp.json();
        }).then((data) => {

            ...
            ... <- some code here
            ...

            res.render('xyz',{categories: categories , ...});
        }).catch((err) => {
            console.log(err);
        });
    }
    catch(err) {
        console.log(err);
    }
});

...
... <- some code here
...

Error I am getting :-

With both Axios and node-fetch I have been getting a common error of

connect ECONNREFUSED XX.XX.XX.XX:8080

Some of the things that I have tried :-

I have switched from Axios to node fetch thought maybe that had to do something with it, I have hosted a new node app on vps that when requested will show a msg in console that a request was made and pass the Json by locally fetching it, when I made a request from postman it worked the console logged 'Request was made' but when I tried it on the cPanel hosted app it did not show anything, I have also tried making my Api a https response but that did not work ether.

Note :-

The app is working fine when i host it in local pc, when i host the node app in cPanel it won't work.

ZeyoYT
  • 11
  • 3
  • Are you sure there is a service listening on xx.xx.xx.xx:8080? And if this xx.xx.xx.xx is a VPS which you manage yourself: Have you configured the firewall to allow connections on port 8080? – derpirscher Oct 29 '22 at 21:49
  • @derpirscher yes, there is a service listening on port 8080 on my vps , the Ip of vps is correct, and I can access the Api from my local pc, the vps is remote server – ZeyoYT Oct 29 '22 at 22:09

1 Answers1

0

Solution Found :-

because I am new to web developing and never used cPanel before, I had to allow my backend vps server Ip after contacting my web server provider he allowed the Ip and now it's working like a charm

ZeyoYT
  • 11
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 02 '22 at 20:14