Thursday, 11 May 2017

Angular on Node JS web service - GET http://127.0.0.1 net::ERR_CONNECTION_REFUSED

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/data), I am now getting GET http://127.0.0.1 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



via Darren Christopher

No comments:

Post a Comment