Error: listen EADDRINUSE :::443 error when trying to start nodejs server - node.js

I am trying to create a nodejs application that uses SSL. I have the cert, domain, and nodejs configured and working, but I'm having a problem sometimes when I stop my node server using Ctrl+C. I am getting the following error when trying to restart the node server:
Error: listen EADDRINUSE :::443
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at Server._listen2 (net.js:1237:14)
at listen (net.js:1273:10)
at Server.listen (net.js:1369:5)
at Object.<anonymous> (/var/www/html/server.js:212:47)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
Now I know this means that the port is in use, but I've tried several things to find the process that is using that port but I cant. A couple thing I've tried that are worth mentioning are netstat:
$ sudo netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1164/sshd
tcp6 0 0 :::22 :::* LISTEN 1164/sshd
udp 0 0 0.0.0.0:68 0.0.0.0:* 882/dhclient
and ps:
$ ps aux |grep node
ubuntu 7632 0.0 0.0 12944 988 pts/0 S+ 23:30 0:00 grep --color=auto node
but neither of them show me what is using port 443. I have pm2 to keep my node server in a running state, but it keeps saying errored and then I find the EADDRINUSE errors in the pm2 error log. I've been banging my head against the wall for hours. Can anyone assist me? Thanks!

I had this issue and the only command that revealed the process was
sudo lsof -iTCP -sTCP:LISTEN -P

Related

https not working in Linux with node/express/webpack

I have a nodejs application, I am able to configure https and run it locally on my windows machin, but when I do the same run on my Linux machin it fails with below error.
EADDRINUSE: address already in use 0.0.0.0:3006
events.js:282
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use 0.0.0.0:3006
at Server.setupListenHandle [as _listen2] (net.js:1308:16)
at listenInCluster (net.js:1356:12)
at doListen (net.js:1495:7)
at processTicksAndRejections (internal/process/task_queues.js:85:21)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1335:8)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: 'EADDRINUSE',
errno: -98,
syscall: 'listen',
address: '0.0.0.0',
port: 3006
}
As suggested by other I tried 'netstat -plnt' but the port 3006 is nowhere used, so nothing to kill.
Not sure if this is code issue or OS issue.
[Update] chnaged the port to 3006
netstat -plnt
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN -
tcp6 0 0 :::111 :::* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
Try using fuser 1223/tcp to get the process and
fuser -k 1223/tcp to kill the process running on the specific port

Port specified when running node is undefined

The port is specified when i ran the node project says undefined.
> sf-chain#1.0.0 dev E:\System\dev\node\sf-chain
> nodemon ./app "HTTP_PORT=3002" "P2P_PORT=5002" "PEERS=ws://localhost:5001"
[nodemon] 1.18.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node ./app HTTP_PORT=3002 P2P_PORT=5002 PEERS=ws://localhost:5001`
process.env.HTTP_PORT :: undefined
HTTP_PORT :: 3001
Listening for peer-to-peer connections on: 5001
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3001
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at Server._listen2 (net.js:1259:14)
at listen (net.js:1295:10)
at Server.listen (net.js:1391:5)
at EventEmitter.listen (E:\System\dev\node\sf-chain\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (E:\System\dev\node\sf-chain\app\index.js:28:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
[nodemon] app crashed - waiting for file changes before starting...
I ran the first instance using
npm run dev
const HTTP_PORT = process.env.HTTP_PORT || 3001;
The application starts successfully running on port 3001 since i didn't specify HTTP_PORT at the run command.
But when trying to run another instance by specifying HTTP_PORT using the below command
npm run dev HTTP_PORT=3002 P2P_PORT=5002 PEERS=ws://localhost:5001
I get this error.
Error: listen EADDRINUSE :::3001
Which means the HTTP_PORT specified at run time is seen as undefined that's why it's trying to using the 3001 port for the first instance.
Error: listen EADDRINUSE
- error comes when somthing is already running/listen on that port
Its because sometime instance of same nodejs acquire port and didnt kill/exit the process properly,
if you have Ubuntu check first which process is using port by
lsof -i :3001 // Port no.
then output will be some thing like this.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 2496 nasiruddin 13u IPv6 37696 0t0 TCP *:3001 (LISTEN)
kill using PID
kill 2496
then start again

Apache cant restart because it already uses its port

Whan I am trying to restart apache it shows an error:
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
But when I am trying to check who uses it, it points to itself:
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 22656/apache2
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 22656/apache2
What is the problem here?
Free the catch memory and
Make sure you are not declaring Listen 80 twice in .conf files.
For example, you might have it both in ports.conf and inn sites-enabled/www.conf.
To find out, use: grep -ri listen /etc/apache2
Keep Listen 80 in just a single place.

cannot run node.js webservice on port 80 or 443

I have a VPS running CENTOS and I'm experimenting with Node.js.
I ran an example node.js server running correctly on various ports, but whenever I tried to run node on port 80 (same error for 443) I get the following error:
root#mic [~/Projects/NodeTutorial2]# node index.js
Server running on port 80.
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:883:11)
at Server._listen2 (net.js:1021:14)
at listen (net.js:1043:10)
at Server.listen (net.js:1109:5)
at Object.<anonymous> (/root/Projects/NodeTutorial2/index.js:8: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)
I've tried to redirect port 80 to 3000 by doing:
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
Apparently I executed this command twice because:
when I did a: sudo iptables -t nat -L, it returned:
root#mic [~]# sudo iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
REDIRECT tcp -- anywhere anywhere tcp dpt:http redir ports 3000
REDIRECT tcp -- anywhere anywhere tcp dpt:http redir ports 3000
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
but to no avail, I still get the same error.
I tried to see what is running on port 80 with the netstat -tulpn | grep:80 command and it returns:
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 477/httpd
tcp 0 0 :::80 :::* LISTEN 477/httpd
So I had killed the process running on port 80 and I got kicked off my VPS immediately.
What ports should I be running my node.js webservice?
According to my putty settings the port I'm accessing is port 22, so I don't understand what is going on there.
Additionally, I'm not planning to run my webservice as root.
I've created another user with less permissions that will run the node.js webservice.
Setup nodejs to use port 8000
install nginx and set it up as a reverse proxy for your nodejs app.
Don't use Apache, it creates a thread/process for every request, while nginx doesn't. It works similar to nodejs event loop where you have a queue of request that need to be processed and nginx worker processes take each request from the queue and process it - in your case send a request to nodejs server and then wait for the response.
And never use nodejs on port 80, there are just too many use-cases to handle and there is no need to reinvent the wheel

How to start node.js on port 80 on a linux server?

When I try to start node on port 80, the error tells me that the port is in use. I imagine that's Apache.
What is the proper way to "take over" port 80, and keep it that way after a server restart?
(Linux xxxx.__.com 2.6.32-5-amd64 #1 SMP Tue Jun 14 09:42:28 UTC 2011 x86_64 GNU/Linux)
you can use ip tables to map port 80 to 8000
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000
to make it permanent
sudo sh -c "iptables-save > /etc/iptables.rules"
and add
pre-up iptables-restore < /etc/iptables.rules
to your /etc/network/interfaces
To take over port 80 when another process is listening on it, you must kill the process (or somehow tell it to stop listening). To ensure that Apache doesn't try to listen on port 80 again the next time it starts, you need to edit its configuration or prevent it from starting up.
To see which process is listening on port 80, run sudo netstat -ntap and look for the row with Local Address ending in port :80. The PID of the process (and the name) is in the far right column.
you can use node.js with node-http-proxy check this link How to use vhosts alongside node-http-proxy? and
How do I run Node.js on port 80?
Thanks & Regards,
Alok
A constantly running unused apache maybe a security hole, in any case no sense in running unused services.
On the chance you're on ubuntu, this what I used..
sudo service apache2 stop
sudo update-rc.d apache2 remove
You can access port 80 once you stop the service currently using it.
In your case, follow these steps:
1) Use systemctl to stop apache2:
sudo systemctl stop apache2
2) Check apache2 status:
sudo systemctl status apache2
Or just by entering http://localhost in your browser. If you get an error, you are good to go.
ERR_CONNECTION_REFUSED
3) Now start your NodeJS server on port 80.
4) You can access your server at http://localhost
UPDATE
If you are seeing errors on your console, try node preceding with sudo
For eg. sudo node server.js
Here are the errors
events.js:137
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Object._errnoException (util.js:1003:13)
at _exceptionWithHostPort (util.js:1024:20)
at Server.setupListenHandle [as _listen2] (net.js:1349:19)
at listenInCluster (net.js:1407:12)
at Server.listen (net.js:1495:7)
at Object.<anonymous> (/home/abdus/Desktop/voice-recognition/test.js:7:4)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)

Resources