NodeJS as a HTTP Service

I want to use node.js on my Ubuntu linode.

I have a basic javascript file 'server.js':

var http = require("http");

var app = http.createServer(function(request, response) {
  response.writeHead(200, {
    "Content-Type": "text/plain"
  });
  response.end("HTTP response from Express.\n");
});

app.listen(3000, "localhost");
console.log("Server running at http://localhost:3000/");

I start the 'server.js' package and should be listening on port 3000. But when I point a browser at my linode's ip:3000 there is no response.

I have tried declaring 'server.js' to listen to port 80, and I've also tried redirecting port 80 to 3000:

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000

This same 'server.js' code snip-it works in my local Windows development environment.

4 Replies

You have explicitly created a localhost only server. Change

app.listen(3000, "localhost");

to

app.listen(3000);

Thanks, I didn't realize that was happening. Works much better without a hostname specified.

Would there be an advantage to setting a hostname?

Hi. I'm having this same problem. That is, when I go to [my ip address]:3000 I get "Oops! Google Chrome could not connect to [mu ip address:3000"

Here's my node.js script:

var http = require("http");

var app = http.createServer(function(request, response) {

response.writeHead(200, {

"Content-Type": "text/plain"

});

response.end("HTTP response from Node.\n");

});

app.listen(3000);

console.log('Running…');

I was wondering if it had something to do with iptables, which I'm only just starting to learn about and don't fully understand.

I added this, thinking it would help, but it didn't: -A INPUT -p tcp --dport 3000 -j ACCEPT

In any case, if it helps, here's the text of /etc/iptables.firewall.rules:

*filter

Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't$

-A INPUT -i lo -j ACCEPT

-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT

Accept all established inbound connections

-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

Allow all outbound traffic - you can modify this to only allow certain traf$

-A OUTPUT -j ACCEPT

Allow HTTP and HTTPS connections from anywhere (the normal ports for websit$

-A INPUT -p tcp --dport 80 -j ACCEPT

-A INPUT -p tcp --dport 443 -j ACCEPT

-A INPUT -p tcp --dport 3000 -j ACCEPT

Allow ports for testing

-A INPUT -p tcp --dport 8080:8090 -j ACCEPT

Allow ports for MOSH (mobile shell)

-A INPUT -p udp --dport 60000:61000 -j ACCEPT

Allow SSH connections

The -dport number should be the same port number you set in sshd_config

-A INPUT -p tcp -m state --state NEW --dport 9973 -j ACCEPT

Allow ping

-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

Log iptables denied calls

-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-$

Reject all other inbound - default deny unless explicitly allowed policy

-A INPUT -j REJECT

-A FORWARD -j REJECT

COMMIT

"

What is the output of the following command:

iptables-save

  • Les

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct