ForeverJS getting EADDRINUSE error - javascript

I'm a novice with Ubuntu and node express. But I've got enough knowledge that I was able to setup a static web server with an REST API running along side it, using Forever. I also successfully installed Samba to edit the files directly from my Windows machine.
About a month ago, I got my static website up and running. It has a form on it that does a jQuery POST to the REST API service. Using Forever, I had two node processes running, one for the static website service, and the other for the API service, BOTH listening on the same port 80.
This was all working fine a month ago, I could load the static website, and submit a form and I would get my email notification via my API service. But about a week ago, everything went down, including my Samba file share. Logged into my server and 'forever list' was empty. Ok, so I figured the server must have restarted without me knowing. I then start up both my services again:
root#xxxxx:~/www/web/ourwedding# forever start staticserver.js
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: staticserver.js
root#xxxxx:~/www/web/ourwedding# forever start apiserver.js
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: apiserver.js
I run forever list:
root#xxxxx:~/www/web/ourwedding# forever list
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] IKkB /usr/bin/nodejs staticserver.js 469 471 /root/.forever/IKkB.log 0:0:0:26.307
data: [1] APxV /usr/bin/nodejs apiserver.js 479 481 /root/.forever/APxV.log STOPPED
I see the error and go to check it out:
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1039:14)
at listen (net.js:1061:10)
at Server.listen (net.js:1135:5)
at EventEmitter.app.listen (/root/www/web/ourwedding/node_modules/express/lib/application.js:595:24)
at Object.<anonymous> (/root/www/web/ourwedding/apiserver.js:69:18)
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)
at startup (node.js:119:16)
at node.js:902:3
error: Forever detected script exited with code: 8
I then spend the next couple of hours trying to figure out what is going on. From what I have found so far, that error is because I'm running both services on the same port... BUT it was working fine before? I have to run them on the same port, because I'm doing a AJAX POST to the same domain.
Things I've tried:
deleting the .forever folder in root
restarting the server
killall -9 nodejs
Things I've not tried:
a different solution to forever
combining my static web server and api server into one service.
I guess I'm just wondering if I'm going crazy or if I did something a month ago that I just cant for the live of me remember, that allowed me to run both services on the same port.
Any help or suggestions would be welcomed.
Thank you.

Argh... and the answer is:
my memory is crap :-)
I decided I should just combine my two services in one, and as I'm going through the code, I notice that I had already combined them!!! I'm such an idiot. Thank you for those that tried to help, but in this case its definitely user error.
As mentioned before, EADDRINUSE is thrown because both services was trying to listen on the same port... I just thought that I had solved it somehow without combining my files.

Related

How to avoid using "kill <PID>" command every time on getting the error: listen EADDRINUSE nodeJS?

I'm running a simple MERN stack application on my local server(macOS). Although I figured out how to kill the process using the below statement, I wonder if there is a way to prevent getting EADDRINUSE error often?
kill -9 <PID>
I've been getting this error very regularly and it's affecting my productivity to kill and restart the server many times. Any insights on it?
Using nodemon v1.18.9 to monitor changes and start the server.

Meteor reset not working in vagrant

I'm trying to get meteor reset to work in vagrant box. However I kept getting errors. This is my error message:
/home/vagrant/.meteor/packages/meteor-tool/.1.1.10.8ogiso++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/lib/node_modules/fibers/future.js:278
throw(ex);
^ Error: EBUSY, rmdir '/vagrant/foosboom-meteor/.meteor/local'
at Object.Future.wait (/home/vagrant/.meteor/packages/meteor-tool/.1.1.10.8ogiso++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/lib/node_modules/fibers/future.js:398:15)
at Object.rm_recursive (/tools/fs/files.js:270:9)
at Command.main.registerCommand.name [as func] (/tools/cli/commands.js:1211:9)
at /tools/cli/main.js:1378:23
I'm using hashicorp Otto as my development environment.
I have performed a mount on the local folder as suggested by one of the user here.
Any idea what seems to be the problem? Thanks.
After mounting you can't reset your mongo so do this
sudo umount -f /vagrant/foosboom-meteor/.meteor/local

How do I get SocketCluster to restart on file change?

I'm running http://socketcluster.io/ and I want to restart my workers whewnever a file changes. However, nodemon server.js fails as soon as it tries to restart with an endlessly-repeating wall of:
1445633138359 - Origin: Worker (PID 44372)
[Error] Error: connect ECONNREFUSED /var/folders/fj/yzfd3_tn7xd0smz7j6s093mh0000gn/T/socketcluster/6879fe94-ed92-4188-b1d7-cb187a5ade4e_b68fcded6c/b0
at Object.exports._errnoException (util.js:874:11)
at exports._exceptionWithHostPort (util.js:897:20)
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
1445633138362 - Worker 0 exited - Exit code: 0
How can I safely restart SocketCluster to load the new changes?
nodemon sends the SIGUSR2 signal to the main (Master) process. SocketCluster (correctly) interprets this as as a request to reboot the workers. Unfortunately, there's an open issue where things are not shut down properly and errors fly all around.
There are two options:
You can add the code from the linked issue:
house.addShutdownHandler(function(ctx, next){
socketCluster.killWorkers();
socketCluster.killBrokers();
next();
});
or use forever to send a "restart everything" signal:
forever -w --killSignal=SIGTERM server.js
Improvements were made for nodemon in SC version 5.0.23 or later.
Make sure that you pass killMasterOnSignal: true when instantiating SocketCluster in your code (server.js file) - This setting is necessary for nodemon to work.

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
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.
A common situation to get this error is when one does the following:
startup something
use Ctrl+z and put it into background
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?
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.
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
If closing the process which is using that port doesn't fix it , try the below solutions.
Installing the below package fixed it for me forever.
npm install ws#3.3.2 --save-dev --save-exact
Run this command in your terminal :
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
For Arch Linux add this line to /etc/sysctl.d/99-sysctl.conf:
fs.inotify.max_user_watches=524288
Then execute:
sysctl --system
https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers#the-technical-details

Meteor on windows - Server crashes on code push

I installed Meteor for windows from here :
http://win.meteor.com
The server crashes almost every time I try to make live code push.
I would then have to end the mongod.exe process and run 'meteor reset' and 'meteor' to start it up back again.
Here are the logs:
=> Meteor server restarted
events.js:71
throw arguments[1]; // Unhandled 'error' event
^
Error: This socket is closed.
at Socket._write (net.js:519:19)
at Socket.write (net.js:511:15)
at Socket.HttpProxy.proxyWebSocketRequest.reverseProxy.incoming.socket.on.listeners.onOutgoing (C:\Program Files\Meteor\lib\node_modules\http-proxy\lib\node-http-proxy\http-proxy.js:515:35)
at Socket.EventEmitter.emit (events.js:96:17)
at TCP.onread (net.js:397:14)
Anyone on the same boat? I read from here https://github.com/meteor/meteor/issues/513 that this has been an open issue for over 6 months.
Someone's got any workaround? Help appreciated.
The EADDRINUSE issue was fixed in 0.6.3.1-rev4, but please update to version 0.6.4 as this problem seems to have resolved the other issue by the update to the http-proxy package.
For MSI downloads see https://github.com/sdarnell/meteor/wiki/Windows
Should also appear on http://win.meteor.com soon
This is a known issue with windows for meteor : https://github.com/sdarnell/meteor/issues/7. The one on meteor's github repo might also be related. I have it too! Hopefully they sort it out soon!. Just ctrl+c to stop your server before you edit code and get it back up after you're done.
Even if you start it after this sometimes you could also get an ADDRINUSE error, which is quite frustrating.

Categories

Resources