简体   繁体   English

Node JS Web 服务上的 Angular - GET http://127.0.0.1 net::ERR_CONNECTION_REFUSED

[英]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.我正在使用 AngularJS 开发网站并将其托管在 NodeJS(Express JS)Web 服务中。 The AngularJS uses the data on some endpoints served by NodeJS. AngularJS 使用 NodeJS 服务的某些端点上的数据。 In another word, the NodeJS has endpoints which serve AngularJS web page and providing data needed.换句话说,NodeJS 具有为 AngularJS 网页提供服务并提供所需数据的端点。

Before host the webpage on my virtual machine (web service), I use my host machine to host it.在我的虚拟机(Web 服务)上托管网页之前,我使用我的主机来托管它。 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.但是,当我将它托管在我的虚拟机(Web 服务)上并更改为在我的本地主机( http://127.0.0.1:3000/data )上请求数据时,我现在得到了GET http://127.0.0.1:3000/data net::ERR_CONNECTION_REFUSED错误。

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 ):首先,我认为这是由于跨源资源共享而发生的,所以我通过添加以下代码行(取自此站点)在我的 NodeJS 中启用它:

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.我相信这是因为我的网页试图访问其 localhost API 上的端点,这应该在我第一次尝试时通过 CORS 处理来克服。

What is causing this exactly if it is not CORS?如果不是 CORS,究竟是什么导致了这种情况? How do I overcome this issue?我如何克服这个问题? Any help would be appreciated.任何帮助,将不胜感激。

Update: Node JS Route Code更新:Node JS 路由代码

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这是我的 AngularJS GitHub 存储库

Turns out using localhost IP is wrong.原来使用 localhost IP 是错误的。 It is probably due to the JavaScript is sent to the client and the browser then interprets localhost is the client's localhost.这可能是由于将 JavaScript 发送到客户端,然后浏览器将 localhost 解释为客户端的 localhost。 Therefore, changing the localhost to my actual IP address solved my problem.因此,将本地主机更改为我的实际 IP 地址解决了我的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular.js $ http拦截“net :: ERR_CONNECTION_REFUSED” - Angular.js $http intercept “net::ERR_CONNECTION_REFUSED” Web3.js - 选项 http://localhost:8545/net::ERR_CONNECTION_REFUSED - Web3.js - OPTIONS http://localhost:8545/ net::ERR_CONNECTION_REFUSED GET http://localhost:5000/user net::ERR_CONNECTION_REFUSED - GET http://localhost:5000/user net::ERR_CONNECTION_REFUSED 我正在获取GET http:// localhost:81 / socket.io / socket.io.js net :: ERR_CONNECTION_REFUSED - I am getting GET http://localhost:81/socket.io/socket.io.js net::ERR_CONNECTION_REFUSED 向“http://localhost:3000/”发出 GET 请求时出错“加载资源失败:net::ERR_CONNECTION_REFUSED ... delete.js” - error when making GET request to 'http://localhost:3000/' “Failed to load resource: net::ERR_CONNECTION_REFUSED … delete.js” Heroku + Node.js + Peer.js(webrtc):无法加载资源:net :: ERR_CONNECTION_REFUSED - Heroku + Node.js + Peer.js (webrtc): Failed to load resource: net::ERR_CONNECTION_REFUSED POST http://localhost:9000/ net::ERR_CONNECTION_REFUSED - POST http://localhost:9000/ net::ERR_CONNECTION_REFUSED Express,http-代理中间件和net :: ERR_CONNECTION_REFUSED - Express, http-proxy-middleware and net::ERR_CONNECTION_REFUSED node.js / jQuery跨域:ERR_CONNECTION_REFUSED - node.js / jQuery cross domain: ERR_CONNECTION_REFUSED net::ERR_CONNECTION_REFUSED 尝试在节点 js 中发出发布请求时 - net::ERR_CONNECTION_REFUSED when trying to make a post request in node js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM