Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js throws er Unhandled error event

I have installed node, npm in my Centos 6 server, and i am using putty for running commands at server.

Node is installed correctly at root and running awesome at anywhere at server.

my project is at /home/shaadise/public_html/Ikon

I have created a hello.js file /home/shaadise/public_html/Ikon

var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World\n');
}).listen(8080);

console.log('Server started');

while running js:

root@vps [/home/shaadise/public_html/Ikon]# node hello.js
Server started

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EADDRINUSE
    at errnoException (net.js:904:11)
    at Server._listen2 (net.js:1042:14)
    at listen (net.js:1064:10)
    at Server.listen (net.js:1138:5)
    at Object.<anonymous> (/home/shaadise/public_html/Ikon/hello.js:6:4)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
root@vps [/home/shaadise/public_html/Ikon]#  throw er; // Unhandled 'error' event
-bash: throw: command not found
-bash: //: is a directory

Question: where i have to put my node js file and how can i access it????

i tested to run command:

root@vps [/home/shaadise/public_html/Ikon]# netstat -plnt | grep ':8080'
tcp        0      0 0.0.0.0:8080                0.0.0.0:*                   LISTEN      27111/nginx
like image 705
Manwal Avatar asked Mar 26 '14 07:03

Manwal


4 Answers

This Error: listen EADDRINUSE categorically means that either you or a daemon is running another application on 8080.

However, to check, try running on a different port?

-edit- as this is getting quite a few upvotes, I thought i'd add a bit of additional debug into it.

Pretty much all node.js tutorials default to port 8080 for running. This is because it is similar to the default port 80 used by other web services, such as Apache or NGinX.

In order to determine if another application is running on the same port, you can use netstat -a to see all active connections and their ports, and then grep that list to find any process connected on the same port as your Node.js application.

It doesn't really matter which port your Node application runs on, as long as it's a free port. Ultimately, when you deploy into production, you would sync up whatever content server you are using (Apache/NGinX) to use the same port.

like image 135
Stephen Wright Avatar answered Sep 30 '22 14:09

Stephen Wright


A common situation to get this error is when one does the following:

  1. startup something
  2. use Ctrl+z and put it into background
  3. try to startup something again

The good way to go would be always try to hit Ctrl+c first which sends signal to the application (which may decide to shutdown). You can read more about it here: What is the difference between Ctrl-z and Ctrl-c in the shell?

like image 38
Marcin Avatar answered Sep 30 '22 14:09

Marcin


The server is running in background; it's happing, usually, when you don't kill the process. To solve this you can put on the terminal:

ps | grep 'node'

This code will show you a process that have a specific number, use the next code to kill the process:

kill -9 "specific number"

You can use sudo if this doesn't work correctly.

like image 44
Danrley Pereira Avatar answered Sep 30 '22 14:09

Danrley Pereira


If you are using Linux based system, first you have to list all the programs that are using that particular port and kill them(meaning stop them)

exemple: I want to list all programs that are using the 3000 port

fuser 3000/tcp 

then pick the process ID, which is in the right side of the obtained line of text and issue the kill command

exemple : if have a process ID with the value of 2345 then the command will be

kill 2345
like image 30
ismnoiet Avatar answered Sep 30 '22 14:09

ismnoiet