Node.js can access default port, but can't access specific port - node.js

http.createServer(function(request, response){
response.writeHead(404, {'Content-Type': 'text/plain'});
response.write('Helloworld!');
response.end();
}).listen(3000,function(){
console.log("server listen on port::: 3000");
});
Here above is a quite simple snippet run in Ubuntu.
I can access normally by default port(80), but can't access by a specific port.
What I accessed is an external IP, not internal localhost.
I can get correct response via command line of "curl x.x.x.x:3000", but I just can't open it in web browsers.
Anybody who can give a hint will be appreciated!!!

A process is probably using the port.
netstat -tulpn | grep 3000 will give you what process is using the port.
If none found, look into your iptables.
Edit: As suggest from comment, probably OP desk that restrict external request for non http ports. Simple iptable will do.
-A OUTPUT -d **dest_ip**/32 -p tcp -m tcp --dport 3000 -j ACCEPT
Replace dest_ip by your ip.

Related

How do I access my node.js server via public IP address?

I want to get a response from a remote node.js server by typing my public IP address into my browser. When I type my public IP into my browser on my personal computer, I get "Unable to Connect". My node.js server isn't connected to the World =(
I am running CentOS on a Linode (but I don't think either choice should matter to my question).
Via Terminal on my person computer (a Mac), I can successfully SSH as root into my Linode.
I have installed node.js successfully on my Linode.
I can compile and run a simple server on my Linode.
var http = require('http');//create a server object:
http.createServer(function (req, res) {
res.write('Hello World!'); //write a response
res.end(); //end the response
}).listen(3000, function(){
console.log("server start at port 3000");
});
I've tried:
Setting a hostname.
Changing the "hosts" file on my server.
Changing the port number in my node.js server (3000, 80, 8080, 3001, 0.0.0.0, etc).
Read literally 100 articles today about how to deploy a node.js server.
Searched Google, Stackoverflow, Linode forums, etc for threads that might help me.
I have zero idea what I'm doing wrong and would be so grateful for your help.
I eventually found the answer, thanks to Saddy's suggestion that the problem might be port forwarding.
1. I decided to use ports 3080 and 3443 for my node server.
2. I SSHed into my CentOs instance.
3. I disabled the default firewall, firewalld.
4. I set up port forwarding using iptables with the following commands:
firewalld stop
firewalld disable
iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT
iptables -I INPUT 1 -p tcp --dport 443 -j ACCEPT
iptables -I INPUT 1 -p tcp --dport 25 -j ACCEPT
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3080
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 3443
iptables-save > /etc/sysconfig/iptables
After this, I was able to access my node server via a browser.

Running a simple script on a web server using NodeJS

I'm trying to make a simple JS script run on my web server using NodeJS :
var http = require('http');
var server = http.createServer(function(req,res) {
res.writeHead(200,{"Content-Type":"text/plain"});
res.end("Hello World!");
});
server.listen(8000);
console.log('Server running');
Output when I run this on local :
curl http://127.0.0.1:8000
Hello World!
However, when I try to curl onto my web server it timeout.
I don't know if it's important or not but I have apache2 installed on the web server. Any help would be very much appreciated.
You can't access node.js from outside because it is listening on localhost IP i.e 127.0.0.1. You need to configure node.js to listen on 0.0.0.0 so it will be able to accept connections on all the IPs of your machine.
Try this:
server.listen(8000, "0.0.0.0");
If it still doesn't work and you are using iptables you may have to open port 8000. Try this:
iptables -I INPUT -p tcp --dport 8000 -j ACCEPT

cannot get nodejs working on amazon ec2 instance

I am trying to run nodejs server on amazon micro instance for the past 3-4 days with no success.
i followed so many tutorials and i cannot make the server work (so when its visited in browser or curl in powerShell and see the simple hello world).
Here are the last 3 tutorials I've tried:
http://iconof.com/blog/how-to-install-setup-node-js-on-amazon-aws-ec2-complete-guide/
http://techprd.com/setup-node-js-web-server-on-amazon-ec2/
http://devblog.daniel.gs/2014/01/deploying-node-apps-on-aws-ec2-with.html
I've tried ubuntu/ amazon linux.
I've also followed http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/install-LAMP.html just to see how the general process works and it did work fine.
I get to the point where i do something like: sudo node filename.js
and the server runs.
However when i try visiting or curl it. it doesnt work. when i ping it it pings fine.
in amazon i set the security group to basically allow everything like so:
in linux its self i followed : https://help.ubuntu.com/community/IptablesHowTo?action=show&redirect=Iptables to open the port i was trying to listen to and nothing.
when i type : sudo service iptables status i get :
1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpts:1337:1347
2 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpts:1338:1348
3 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:10000
keep in mind i am not a server savvy guy.
Any help would be highly appreciated.
UPDATE: To start my node server i use the basic code found in nodejs.org :
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
This is exactly how my file look like
You haven't provided enough info, but I'll try and outline the general troubleshooting techniques.
Is your node program running and serving requests? Get on the Ec2 box and do curl -v localhost:8000 (or whatever your node port is.) It should spit out your app. If not, your node.js setup isn't right.
Is your nginx running? Get on the box and type curl -v localhost. That will look on your port 80. If it doesn't proxy to your app, it's probably an nginx configuration problem.
Can you reach the box via IP address? From your desktop/laptop, do curl -v x.x.x.x where x.x.x.x is your EC2 Public IP address. If that hangs, there is a firewall problem.
Can you reach the box via your DNS? (Assuming you are trying to have foo.com point to it) Try curl -v http://foo.com/. If that doesn't work, your DNS may not be correct.
However when i try visiting or curl it. it doesnt work.
Please be more specific.

netstat commands to run on unix server, what commands should I use for my use-case and why?

Sorry in advance for such a noob question, but I'm certainly a noob.
My question is what does it mean to LISTEN or ACCEPT on a port as it relates to my example?
EXAMPLE:
I have a tomcat server, and It will use port 8080. I want to make sure that port is available for me to use.
What commands should I perform on my unix server and why?
what information would a command like this give me: netstat -an | grep LISTEN
If a port shows up as LISTEN in netstat, it means the port is in use by a server process, so you can't use it. Here is an example:
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
which shows that port 631 is in use.
Ignore the UNIX type sockets at the end - they are irrelevant.
For checking port 8080 is in use or not, you can simply use the command netstat -an|grep 8080. If you get an output in below format, that means 8080 is already in use and you need to assign a new port for the tomcat.
# netstat -an
tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN
Netstat command displays various network related information such as network connections, routing tables, interface statistics, masquerade connections, multicast memberships etc,
a option with netstat will give you both listening and non listening ports
n option when you don’t want the name of the host, port or user to be displayed, use netstat -n option. This will display in numbers, instead of resolving the host name, port name, user name. This also speeds up the output, as netstat is not performing any look-up.
For more understand the use of netstat command here are its options:
-a : All ports
-t : Ports TCP
-u : Ports UDP
-l : Listening ports
-n : IP address without domain name resolution
-p : Name of the program and it associated PID
So:
-To display all port (TCP & UDP), PId with the associated name of the program :
$ netstat -paunt
-To display all Listening ports (TCP), PId with the associated name of the program : (and we can also filter with the grep command)
$ sudo netstat -plnt | grep ':80'
I hope it will be helpful :)
You can also use telnet to check if the port is open and listening e.g,
Zeeshan$ telnet google.com 80
Trying 173.194.35.5...
Connected to google.com.
Escape character is '^]'.
I am telnetting google.com on port 80. If you see the third line in the output, you will notice it says it is connected with the Google's web server. The same way you have a JAVA application server called Tomcat and it is listening on port 8080. In fact it is asking clients to connect to it on port 8080 so it can give away the JAVA services to client. When I will use from a client side telnet localhost 8080 I will be connected the same way I have connected with Google's web server on port 80. Provided that Tomcat is running and listening on port 8080. If port 8080 is not free and occupied by some other application you can simply change the port 8080 to another free port. Telnet should give you the following status:
accepted (connected), refused, and timeout
connection refused - nothing is running on that port
accepted - some application is running on the port
timeout - a firewall is blocking access
So now there are two possible ways to check. From the same machine you are running Tomcat server:
telnet localhost 8080
Of if you want to check it from some other machine or outside of the network:
telnet 192.168.1.1 8080
I hope that helps.
use can also run the below command, it will list the Port and corresponding PID, if any process is using those ports
netstat -tulpn

Can't connect to nodejs server

I run Apache on my server. Going to my address x.x.x.x:port loads the index.html page in /var/www. When I stop the server, I can no longer connect (all good).
Now I start the node server with node server.js (the server.js file below is also located in /var/www).
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(port, 'x.x.x.x');
console.log('Server running at http://x.x.x.x:port/');
This gives the error listen EADDRNOTAVAIL, but I am not running any other node server (there is no other process running at this port).
I have also tried omitting the IP address and just listening thus: listen(port);
This returns no errors, but I cannot connect to the server (Browser says: Firefox can't establish a connection to the server at x.x.x.x:p.)
I have found out the problem. You don't need to specify a host name:
listen(port, 'x.x.x.x')
should just be
listen(port)
otherwise the server will not accept any connection except ones directed at the specified ip.
The port is in use or not available. Try a different port like:
listen(88, 'x.x.x.x');
and see if that connects. Also, make sure that x.x.x.x is actually the ip address of your server. You can listen on all IPs by doing:
listen(88, '0.0.0.0');
or by leaving the host/ip section out entirely. If it does connect on another port, you just need to find what is using the port you want. If it's port 80, use:
sudo netstat -tulpn | grep :80
to get the program using that port.
Sounds like the port is locked up and in use..
The following command will give you a list of node processes running.
ps | grep node
To free up that port, stop the process using the following.
kill <processId>

Resources