NodeJS as a HTTP Service
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
app.listen(3000, "localhost");
to
app.listen(3000);
Would there be an advantage to setting a hostname?
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
"
iptables-save
- Les