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.