1

I installed node.js on a hosted Apache server. The simple server I placed on the server runs fine, but when I go to the website I cannot see the website.

I initially tested this on my local machine and it works fine, but I need this on a production website. How can I do this.

My Node.js code

[code]
// Load the net module to create a tcp server.
var net = require('net');

// Setup a tcp server
var server = net.createServer(function (socket) {

  // Every time someone connects, tell them hello and then close the connection.
  socket.addListener("connect", function () {
    sys.puts("Connection from " + socket.remoteAddress);
    socket.end("Hello World\n");
  });

});

// Fire up the server bound to port 7000 on localhost
server.listen(1337, "localhost");
[/code]

// Put a friendly message on the terminal console.log("TCP server listening on port 1337 at localhost.");

Then I run node test.js Response : TCP server listening on port 1337 at localhost.

Then I go to www.mywebsite.com:1337

Oops! Google Chrome could not connect to www.mywebsite.com:1337

So I tried using the actual IP server.listen(1337, "xx.xx.xx.xx");

And the URL server.listen(1337, "http://mywebsite.com"); // this actually broke the server immediatly

So how can I do this?

  • whats the output of `iptables -t filter -L `? – Chris Jan 01 '13 at 17:31
  • Too long to copy and paste. – user1941252 Jan 01 '13 at 18:01
  • Mostly DROP all -- anywhere xx.xx.xx.xx anywhere – user1941252 Jan 01 '13 at 18:02
  • Try removing the host argument so it can listen to [`INADDR_ANY`](http://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback) rather than just `127.0.0.1`: `server.listen(1337);`. Or try: `server.listen(1337, "mywebsite.com");` (difference being server name only; no `http://` protocol). Also ensure that your host allows connections on port `1337`. Depending on your hosting plan, the list of supported port #s may be rather short. If you have "private" or "dedicated" hosting, you may be able to alter the firewall yourself to allow `1337`. – Jonathan Lonowski Jan 01 '13 at 19:23

1 Answers1

0

You will need a firewall rule to allow incoming traffic.

iptables -A INPUT -p tcp --dport 1337 -j ACCEPT

and do not bind to localhost, but on the port only:

server.listen(1337/*, "localhost"*/);

http://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback

EDIT: This comments out the host, so your server will listen on all adresses (this is the same as:)

server.listen(1337);

If you still encounter problems, this is most likely a firewall problem.

Chris
  • 4,204
  • 1
  • 25
  • 30
  • My provider opened the port I specified. So apparently they do close unused ports. But I still get the same error. The previous post is essentially commenting out the host.../*how does that help*/ ? – user1941252 Jan 06 '13 at 11:11
  • sorry, my frustration got the better of me. The url post makes sense, but the port was specifically set by my provider for this service.. – user1941252 Jan 06 '13 at 11:39
  • the change is actually not about the port but the address youre listening on – Chris Jan 06 '13 at 12:42