最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access remote node.js app in browser, not on localhost - Stack Overflow

programmeradmin1浏览0评论

I'm trying to set up a simple "Hello world" node.js app.

I've created the following index.js file:

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. I point my browser to localhost:8080, and I see the HTML page saying "Hello worlddd", as desired.

Now, I'm trying to do the same on my Virtual Private Server, so I can access the same app from different puters, but all I get is connection timeouts. I've followed these steps:

  • Install node.js on my VPS
  • Install express via npm install --save [email protected]
  • 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).
  • Access the server via PuTTY, and run node index.js, where I get "listening on *:8080", so I know node.js is working.
  • 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".
  • I've tried listening to port :80 instead, but I get the error that this port is already in use.

Does anybody know what I'm doing wrong? Am I using the wrong port? Am I pointing to the wrong URL? Do I need to modify my server preferences? (running Apache on CentOS). I've only found dozens of tutorials that teach you how to run a node.js app on your local puter(pointing the browser at localhost:8080), but I need it to run on my remote server so multiple puters can access the same app.

I'm trying to set up a simple "Hello world" node.js app.

I've created the following index.js file:

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. I point my browser to localhost:8080, and I see the HTML page saying "Hello worlddd", as desired.

Now, I'm trying to do the same on my Virtual Private Server, so I can access the same app from different puters, but all I get is connection timeouts. I've followed these steps:

  • Install node.js on my VPS
  • Install express via npm install --save [email protected]
  • 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).
  • Access the server via PuTTY, and run node index.js, where I get "listening on *:8080", so I know node.js is working.
  • 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".
  • I've tried listening to port :80 instead, but I get the error that this port is already in use.

Does anybody know what I'm doing wrong? Am I using the wrong port? Am I pointing to the wrong URL? Do I need to modify my server preferences? (running Apache on CentOS). I've only found dozens of tutorials that teach you how to run a node.js app on your local puter(pointing the browser at localhost:8080), but I need it to run on my remote server so multiple puters can access the same app.

Share Improve this question asked Feb 25, 2016 at 23:34 M -M - 28.6k12 gold badges54 silver badges93 bronze badges 11
  • 1 You need to find specific documentation for running a node.js server app on your VPS and how to configure the ining port and whether long running servers (like nodejs) are supported. This is not generic instructions, but likely involves specific configuration for your own hosting service. – jfriend00 Commented Feb 25, 2016 at 23:46
  • Oh, thanks! So it's not expected to work straight out of the box, then? Maybe my hosting provider's customer care center can help out. – M - Commented Feb 25, 2016 at 23:48
  • There may be simply some (default) filtering going that prevents connections to other ports that the predefined ones. Or if you share the IP with other hosts, it might be quite a bit more plex. – jcaron Commented Feb 25, 2016 at 23:49
  • 2 Check for filters (probably iptables if it's some kind of Linux-based server). If you have any kind of control panel, it's probably configurable somewhere in there. – jcaron Commented Feb 26, 2016 at 1:21
  • 1 @jcaron OH MY GOSH, I GOT IT! Thanks so much! iptables definitely sent me in the right direction, since it's an Apache CentOS server. I ended up using the following mand: sudo iptables -I INPUT 1 -i + -p tcp --dport 8080 -j ACCEPT. Does using the wildcard + make me vulnerable to any security issues? I still don't fully understand what that interface parameter does, despite reading the Ubuntu Iptables Howto page. – M - Commented Feb 26, 2016 at 2:28
 |  Show 6 more ments

1 Answer 1

Reset to default 4

The issue is that your current filters (iptables) block traffic unless you explicitly allow it.

You just need to open port TCP 8080 inbound, and you should be able to reach your node.js server!

发布评论

评论列表(0)

  1. 暂无评论