4

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 computers, but all I get is connection timeouts. I've followed these steps:

  • Install node.js on my VPS
  • Install express via 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).
  • 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 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.

M -
  • 26,908
  • 11
  • 49
  • 81
  • 1
    You need to find specific documentation for running a node.js server app on your VPS and how to configure the incoming 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 Feb 25 '16 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 - Feb 25 '16 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 complex. – jcaron Feb 25 '16 at 23:49
  • There should be plenty of online developer doc on how to set up node.js for your particular hosting provider. You just need to find that doc. The issue is that they're usually trying to share resources among multiple tenants and how exactly they do that effects how you must set up your server. For example, they will often expect you to put your server on a specific port number that they will forward port 80 to or you have to set up some custom configuration to listen to something other than port 80 from the outside world. – jfriend00 Feb 25 '16 at 23:51
  • @jcaron That's starting to make sense. So I wasn't wrong in expecting it to work at `http://192.123.123.12:8080` then, it's just that my server settings aren't allowing it at the moment? – M - Feb 25 '16 at 23:51
  • If you try to connect to your server via it's IP address (rather than name), do you get to your server? If so, you most probably have a dedicated IP address, and it should be possible to reach your server via `http://ip:8080` (or `http://domain:8080`), but there's something between you and your server that blocks port 8080. It might be your server, it might be a filter/firewall set up by your host that you can configure, or it might be a filter/filterwall set up by your host that you cannot configure. Start by checking your server settings, and work from there. – jcaron Feb 26 '16 at 00:45
  • @jcaron Yeah, I'm starting to think it's some sort of setting I need to adjust. Whenever I add an `index.html` and point to `http://ip` I see the index.html page as expected. It's when I point to `http://ip:8080` (or I've also tried `http://ip:3000`) that I get a timeout. I'll try talking to customer care, and see if they can shine some light on the issue. They say I have root access to the server, so hopefully that helps me in some way! – M - Feb 26 '16 at 00: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 Feb 26 '16 at 01: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 command: `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 - Feb 26 '16 at 02:28
  • Not a big user of `iptables` (I'm more of a FreeBSD guy myself), so I wouldn't know. But you can open a new question for this. – jcaron Feb 26 '16 at 02:31
  • I've added an answer that summarises the solution, feel free to accept and/or up vote it if that helped. – jcaron Feb 26 '16 at 02:33

1 Answers1

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!

jcaron
  • 17,302
  • 6
  • 32
  • 46
  • 3
    and for anyone going to try and see if this is the issue, simply try to telnet to the server on that port, e.g. `telnet myserver.com 8080`. If you get a message that says your connected, the port is open. – Brad Parks Feb 26 '16 at 02:36