I keep getting EADDRINUSE when deploying a simple node server - linux

okay this is really strange , i am getting EADDRINUSE when implementing a simple server ,
i have changed the ports
looked for the already running node js process
tried using killall -9 node with root
looking for process hoarding the port through sudo netstat -alpn |grep node
var http= require('http');
var server=http.createServer(function(request,response){
response.end("kameha kameha....");
}).listen("162.x.x.38",8010);
process.stdin.resume();
process.on('uncaughtException',function(err){
console.log("closing due to uncaught exception:"+err.message+" "+err.data);
server.close();
process.exit(1);
});
process.on('exit',function(){
console.log("exiting the program");
server.close();
process.exit(0);
});
process.on('SIGINT', function(){
console.log("caught cntrl +c closing the program and performing cleanups");
server.close();
process.exit(0);
});
still no help , i am running centos 6.7 on a vps hosted by bluehost , so let me sum is down, there is no node process running , no prcocess is hoarding the port
strangely enough when i put my host as localhost it starts working but still i am unable to connect it via wget
wget localhost:8010
--2015-05-22 15:19:15-- http://localhost:8010/
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:8010... failed: Connection refused.
Connecting to localhost|127.0.0.1|:8010... failed: Connection refused.
when running as local host netstat gives the following :
sudo netstat -alp |grep node
unix 2 [ ACC ] STREAM LISTENING 453187 23439/node localhost
protocol as stated by netstat command is unix instead of tcp ,though i dont have much experience with linux so ....but its strange because above code is running fine in windows , but i dont know what is happening here ..help please !!

The parameters for http.listen() were backwards.
From the node.js docs:
server.listen(port[, hostname][, backlog][, callback])# Begin
accepting connections on the specified port and hostname. If the
hostname is omitted, the server will accept connections directed to
any IPv4 address (INADDR_ANY).
The correct call, from the docs link above, is as follows:
Simple, works on any address for the host
var server=http.createServer(function(request,response){
response.end("kameha kameha....");
}).listen(8010);
Bind to localhost only
var server=http.createServer(function(request,response){
response.end("kameha kameha....");
}).listen(8010, "127.0.0.1");
Bind to 1.2.3.4 is similar
var server=http.createServer(function(request,response){
response.end("kameha kameha....");
}).listen(8010, "1.2.3.4");

Related

nodejs app wont create server, says ERR_CONNECTION_TIMED_OUT

I'm trying to run a nodejs app on my VPS but for somereason I get a ERR_CONNECTION_TIMED_OUT error.
Here is my code:
const http = require('http');
const server = http.createServer((req, res) => {
res.end();
});
server.on('clientError', (err, socket) => {
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
server.listen(8000);
when i cd to my directory and do
node index.js
, it gives me no error, but going to the browser and doing: mysite.com:8000 gives me nothing. The connections times out
EDIT:
when i do curl: enomshop.tk:8000, I get some feedback. Its like i can access from the within the VPS but no access publicly
This is the way you should go about resolving such issues
Check if the process is running
Run ps aux | grep <process> to check if your process is running
Check if the port is being used
Run sudo netstat -plant | grep <port> to check if port is listening or not and is it your process
Check if the service is responding locally
Run telnet 127.0.0.1 <port> and make sure you get a response. If you don't get a response then there are few possible issues you can look into
The process is not started the server listening
The process is not using the correct port
There is a firewall that is blocking the port. There is this for firewall-cmd, and this for ufw
Check if the service is responding externally
You can do telnet <external ip> <port>, if it doesn't work then there few things you should check below
Make sure your server is binding to 0.0.0.0 or <private ip> and not 127.0.0.1
Make sure the VPS server you are hosting on, has your <port> enabled for communication. This is different for each service provider
If you follow these steps, it would solve most common network issues in such cases
I was facing the same issue. To resolve that, just open up / allow the port from network security group (VPS) or Firewall to worldwide access.
And check telnet mysite.com port, it worked for me. Kindly let me know if you still face any issue, I will try to help further.
You will need to open up the 8000 port on your VPS or redirect the traffic from any open port on VPS to 8000

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

Running node app digitalocean and accessing it

Im running my node app with grunt on a DO droplet. I start the server
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:3000
But when I navigate to my dropletIP:3000 I cannot see the app, I get:
This site can’t be reached
mydropletIP refused to connect.
Shouldn't my app be available? I don't have nginx or anything installed.
I was having similar problem but the solution was simple. Change from 'localhost' to '0.0.0.0' i.e
.listen(8080, '0.0.0.0');
And then to test your api just enter your static ip of droplet with port that you have entered i.e droplet-ip:8080
Check the particular port is opened or not ,using following command
sudo ufw status
If any Firewall enabled and any port is block means you can see that.
netstat -an | grep "LISTEN " ( List of listening port on server)
I need this info ,then only we can find a problem

I cant connect to my Amazon ec2 running nodejs using tcp

Hi all I have created an Ubuntu EC2 instance and have installed nodejs on it. Am running a simple node js script (which IS running and not throwing any errors:
var net = require('net');
var server = net.createServer(function (socket) {
socket.write("from server\r\n");
socket.pipe(socket);
});
server.listen(8000, "localhost");
console.log("TCP server listening on port 8000 at localhost.");
to test this, (am on windows) I am running a program called hercules to attempt a tcp connection, but it always comes back with tcp connection error: 10061
and the IP address can't be pinged either.
My ec2's IP address is 54.76.31.140. I have added an inbound tcp:8000 0.0.0.0 rule to my security group in the aws console and I have added hercules to my windows firewall and avg exceptions. I have also added an exception to my home dsl router and finally I have checked the ubuntu iptables there are no rules set.
Pls help, I can't seem to find whats wrong here.
If you tell the server to listen on "localhost," that's the only place it will listen -- "localhost" -- the loopback interface, 127.0.0.1, which is only accessible from... the local host.
Remove the 2nd argument to server.listen().
http://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback
If you want to be able to ping your instance, you have to allow ICMP in the security group.
Also, before you fix it, and after, run this, and note the difference in output:
$ netstat -a -n | grep 8000 | grep -i tcp

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