I am developing website using AngularJS and host it within NodeJS (Express JS) web service. The AngularJS uses the data on some endpoints served by NodeJS. In another word, the NodeJS has endpoints which serve AngularJS web page and providing data needed.
Before host the webpage on my virtual machine (web service), I use my host machine to host it. The webpage then tried to request the data to my virtual machine's endpoint and it works fine.
However, when I'm hosting it on my virtual machine (web service) and changed to request data on my localhost (http://127.0.0.1:3000/data), I am now getting GET http://127.0.0.1:3000/data net::ERR_CONNECTION_REFUSED error.
What I have tried
First, I thought it was occurred because of cross origin resource sharing, so I am enabling that in my NodeJS by adding these lines of code (taken from this site):
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
However, it still does not help at all. I am still getting the same error message.
Later, I thought it probably my firewall was blocking the access. I then turned off my firewall and it still failed to fix my problem.
I believe it happened because of my webpage trying to access endpoint on its localhost API, which should be overcome with CORS handling on my first attempt.
What is causing this exactly if it is not CORS? How do I overcome this issue? Any help would be appreciated.
Update: Node JS Route Code
var express = require('express');
var app =express();
app.use(express.static(path.join(__dirname, 'public')));
app.get('/admin/login/:user/:pass', function(appReq,appRes){
appRes.status(200).send('1');
appRes.end();
});
app.get('/*', function(appReq, appRes){
fs.readFile('./public/index.html', function(err,data){
if (err) {
appRes.writeHead(500);
appRes.end('Error loading html');
}
else {
appRes.writeHead(200);
appRes.end(data);
}
});
});
app.listen(port, function(){
console.log('express app listening at http://'+hostname+":"+port+'/');
});
Here is my AngularJS GitHub repo