Express/HTTP listen EADDRINUSE for every port? - node.js

This is quite weird i tried at least 30 ports on a CentOS 7 server for the express application and i keep getting:
Uncaught Exception: listen EADDRINUSE :::1238
[ERROR] Stack Trace: Error: listen EADDRINUSE :::1238
at Object.exports._errnoException (util.js:1026:11)
at exports._exceptionWithHostPort (util.js:1049:20)
at Server._listen2 (net.js:1253:14)
I tried various different ports and i am pretty sure that nothing is using these ports but i keep getting this error.
Here is my express code:
import http from 'http';
import express from "express";
import compression from 'compression';
import SocketIO from "socket.io";
let App = express();
let HTTPServer = http.Server(App);
let Socket = new SocketIO(HTTPServer);
App.use(compression({}));
HTTPServer.listen(1238, function() {
console.log("Listening..");
});
I checked firewall is off, nothing should block the application. I have one more application runnig on port 8080 just fine. And i just can't get this one working. Any thoughts?

Maybe your process doesn't stop while you are stopping your program in those ports.
try to run these commands in your terminal.
lsof -i:<your port number>
If you got a table then it means your process is not killed, copy that PID from that table and run this command
kill -9 <PID>
this will kill the process running on that port, after this, you can use this port for your program.
I have tested these commands in ubuntu, I don't know about CentOS, but both are Linux so you can try this.

Related

NodeJS: How to stop a web server?

I'm using the plugin gulp-server-livereload.
var server = require('gulp-server-livereload');
gulp.src(pathDir).pipe(server({
livereload: true,
directoryListing: true,
open: true,
port: 80
}));
How to stop the server?
I tried server.restart(), server.kill(), server.reset()
But always there are such errors:
Uncaught Error: listen EADDRINUSE 127.0.0.1:35729
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at Server._listen2 (net.js:1262:14)
at listen (net.js:1298:10)
at doListening (net.js:1397:7)
at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:62:16)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:81:1
0)
Please help me to solve my question or tell me another option how you can
implement what I need.
This error is occurring because of another process using the port. It is likely just a previous instance of this process, however it is possible that another application is using the port.
You will be unable to stop this using Node, as it is a process remaining from another run and processes do not have the permissions to 'manage' one another.
On Windows use task manager to kill the node process, on Mac you can use Activity Monitor, and on Unix/Linux you can use htop or similar.
Alternatively on Unix/Linux (incl. MacOS) you can use
sudo lsof -i :35729
to find the process id of the process ID using that port, then kill it:
kill -9 {PID}
To ensure your node server cleanly shuts down in future add event handlers:
process.on('SIGTERM', ..)
process.on('uncaughtException', ..)
and invoke code inside these to shut down your application.

500 Internal Server Error for URL: http://localhost:8100/api/timeline

500 Internal Server Error for URL:http://localhost:8100/api/timeline dev4living/HeyCommunity
I am trying to run a dev4living/HeyCommunity webApp on a local host but i'm getting the following error:
Error: connect EADDRINUSE 127.0.0.1:8100
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
http://localhost:8100/
-i used the lastest update dev-playing : dev4living/HeyCommunity-backend and dev4living/HeyCommunity-frontendenter code here
-What should i do to fix this error?
-Your assistance will be very much appreciated.
The error states that the port you are trying to use is already occupied. Please check the app running in the port 8100 and stop that and try to run the app again. Changing the port number to something else apart from 8100 will also solve the problem.
The error is pretty self-explanatory buddy. It just means that the address you have used, i.e, the port number is currently in use by something else.
2 possible solutions:
Simply change the port number
using cmd to get the list of processes running. Identify the process running on that port. And use taskkill -pid command. Refer this stack overflow post for details regarding this method

Everytime I run cluster.fork(), I get a Error: bind EADDRINUSE

I'm using node.js, and using the cluster module. Everytime I run cluster.fork(), I always get a
throw er; // Unhandled 'error' event
Error: bind EADDRINUSE
at exports._errnoException (util.js:746:11)
at cb (net.js:1205:33)
at rr (cluster.js:592:14)
at Worker.<anonymous> (cluster.js:563:9)
at process.<anonymous> (cluster.js:692:8)
at process.emit (events.js:129:20)
at handleMessage (child_process.js:324:10)
at Pipe.channel.onread (child_process.js:352:11)
I've been googling this, and I have no idea how this is happening because I'm not passing in any port numbers.
Thanks
EDIT: Posting code
var setupWorkers = function() {
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < 5; i++) {
cluster.fork();
}
}
and this is a function that is called in the app.js which I run by calling node app.js
I was starting a server more than once with all the threads so the port was bound already
The stack trace you provide indicates that EADDRINUSE is coming from the net module. EADDRINUSE typically means that you are trying to listen on an IP/port combination more than once. So, for example, if this is a clustered web server, perhaps all your workers are trying to bind to port 80 on the same IP address. Without more code, it's impossible to tell what's happening.
The example code you gave in the subsequent comment does not trigger EADDRINUSE for me. Instead it errors with cluster.fork is not a function because there's no check for cluster.isMaster before calling cluster.fork().

Nodejs: Listen on port 80 after using setuid/setgid

I just got convinced by some internet articles, that using setuid/setgid to switch to a lower privileged user might be important. Since I am developing a web app, I decided to go for www-data.
So I am using the userid NPM module to figure out the user and group ID of www-data, and then change to it. However, when I do that - and it doesn't matter where entirely - I get the following (in this example, the security handler was executed at the very, very bottom of the code):
2014-09-04T23:07:05.812Z - info: BIRD3 Security -> Changed to www-data:www-data (33:33)
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1023:19)
at listen (net.js:1064:10)
at net.js:1146:9
at dns.js:72:18
at process._tickCallback (node.js:419:13)
at Function.Module.runMain (module.js:499:11)
at startup (node.js:119:16)
at node.js:906:3
As you can see, as soon as I change my privilege level, it drops the accessibility to port 80 too.
Is there a way how I can implement security but keep using port 80 without using something like this?
You don't need a third-party module to do that. process.setgid() and process.setuid() both accept either an ID or a groupname/username.
Also make sure you are dropping the privileges AFTER listening on port 80 and that you call process.setgid() before process.setuid().
Example:
var net = require('net');
var srv = net.createServer(function(s) {
});
srv.listen(24, function() {
console.log('listening');
});
process.setgid('www-data');
process.setuid('www-data');
Isn't the setgid() and setuid() supposed to run after connecting, within the callback response?
srv.listen(24, function() {
console.log('listening');
process.setgid('www-data');
process.setuid('www-data');
});

Node.js Unhandled 'error' event when using http.createServer().listen() on Ubuntu 12.04

Salam (means Hello) :)
I've developed a node.js script on my windows seven machine and it's working fine. but when I run it on my Ubuntu 12.04, the following error shows up and halts my app:
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1020:19)
at listen (net.js:1061:10)
at Server.listen (net.js:1127:5)
at Object.start (/httpServer/httpServer.js:9:34)
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)
and the point that caused error is .listen(80) in this line:
http.createServer(onRequest).listen(80);
^
I've also tried some other port numbers (like 100, 300, 500,...) instead of 80 and the error was still the same.
On Ubuntu you can't listen on ports < 1024 without root privileges. Try running node under sudo.
sudo node app.js
You probably have apache running on port 80, so it's conflicting.
Use another port (NOT within 0-1023), or disable apache.
Cheers
You probably have something else running on port 80, so it's conflicting.
Read here to find out what is using port 80 and stop it
http://www.cyberciti.biz/faq/find-linux-what-running-on-port-80-command/
Usually it means another server like apache is enabled. so stop it.
sudo service apache2 stop
or You have npm start already running in another terminal
or skype is running. in which case go to settings and change it's port. logout n login
Go to Tools -> Options -> Advanced -> Connections and uncheck the box "use port 80 and 443 as alternative".src
or use another port
http-server -a localhost -p 8000
I suggest to install the latest node packets, maybe directly from Node.js server, maybe compiling it.
Try to set a port which is not reserved to any service, like 3700.
Could be heplful to see some other fragment of code, though.
This can also be caused if you have something else already listening on that port - you can try changing the port from the typical default 80 to something more like 10014 and see if that helps!
I was able to rectify the error by explicitely mentionining the "IP address" along with the port when listening to the server.

Resources