简体   繁体   English

如何在浏览器而不是localhost上访问远程node.js应用

[英]How to access remote node.js app in browser, not on localhost

I'm trying to set up a simple "Hello world" node.js app. 我正在尝试建立一个简单的“ Hello world” node.js应用程序。

I've created the following index.js file: 我创建了以下index.js文件:

var app = require("express")();
var http = require("http").Server(app);

app.get("/", function(req, res){
    res.send("<h1>Hello worlddddd</h1>");
});

http.listen(8080, function(){
    console.log("listening on *:8080");
});

When I open up my local console, and perform node index.js , I get the message "listening on *:8080", as expected. 当我打开本地控制台并执行node index.js ,如预期的那样,我收到消息“正在*:8080监听”。 I point my browser to localhost:8080, and I see the HTML page saying "Hello worlddd", as desired. 我将浏览器指向localhost:8080,然后根据需要看到HTML页面显示“ Hello worlddd”。

Now, I'm trying to do the same on my Virtual Private Server, so I can access the same app from different computers, but all I get is connection timeouts. 现在,我试图在虚拟专用服务器上执行相同的操作,以便可以从不同的计算机访问同一应用程序,但是我得到的只是连接超时。 I've followed these steps: 我已按照以下步骤操作:

  • Install node.js on my VPS 在我的VPS上安装node.js
  • Install express via npm install --save express@4.10.2 通过npm install --save express@4.10.2
  • Upload my index.js file to the var/www/html folder on my server with IP 192.123.123.12 (an example, this isn't my real IP). 将我的index.js文件上传到IP 192.123.123.12的服务器上的var/www/html文件夹中(例如,这不是我的真实IP)。
  • Access the server via PuTTY, and run node index.js , where I get "listening on *:8080" , so I know node.js is working. 通过PuTTY访问服务器,并运行node index.js ,在这里我得到"listening on *:8080" ,因此我知道node.js可以正常工作。
  • Now I point my browser to http://192.123.123.12:8080 and after about 20 seconds, I get the browser error: "The connection has timed out". 现在,我将浏览器指向http://192.123.123.12:8080 ,大约20秒后,出现浏览器错误:“连接已超时”。
  • I've tried listening to port :80 instead, but I get the error that this port is already in use. 我尝试改用监听端口:80,但是出现错误消息:该端口已被使用。

Does anybody know what I'm doing wrong? 有人知道我在做什么错吗? Am I using the wrong port? 我使用的端口错误吗? Am I pointing to the wrong URL? 我指向错误的URL吗? Do I need to modify my server preferences? 我需要修改服务器首选项吗? (running Apache on CentOS). (在CentOS上运行Apache)。 I've only found dozens of tutorials that teach you how to run a node.js app on your local computer(pointing the browser at localhost:8080), but I need it to run on my remote server so multiple computers can access the same app. 我只找到了几十个教程,教您如何在本地计算机上运行node.js应用程序(将浏览器指向localhost:8080),但是我需要它在我的远程服务器上运行,以便多台计算机可以访问同一台计算机应用程序。

The issue is that your current filters (iptables) block traffic unless you explicitly allow it. 问题是除非您明确允许,否则您当前的过滤器(iptables)会阻止流量。

You just need to open port TCP 8080 inbound, and you should be able to reach your node.js server! 您只需要打开端口TCP 8080入站,就可以访问node.js服务器!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM