Node Server Running on Port 80 errors with using PM2 - node.js

I have an express node server running on aws ec2 instance. When I run
sudo node server.js
the server runs on port 80 and works fine when I make an http request to the server. However, when I run
sudo pm2 start server.js
I get the following error whenever I make a request
{ Error: listen EACCES: permission denied 0.0.0.0:80
at Server.setupListenHandle [as _listen2] (net.js:1242:19)
at listenInCluster (net.js:1307:12)
at Server.listen (net.js:1395:7)
at Object.<anonymous> (/home/ubuntu/evercampus-server/server.js:19:8)
at Module._compile (internal/modules/cjs/loader.js:816:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
at Module.load (internal/modules/cjs/loader.js:685:32)
at Function.Module._load (internal/modules/cjs/loader.js:620:12)
at Object.<anonymous> (/usr/lib/node_modules/pm2/lib/ProcessContainerFork.js:32:23)
at Module._compile (internal/modules/cjs/loader.js:816:30)
code: 'EACCES',
errno: 'EACCES',
syscall: 'listen',
address: '0.0.0.0',
port: 80 }
I tried using pm2 to run on port 5000 and it works fine.
I am very confused on why sudo node server.js works but sudo pm2 start server.js does not.
I saw on another post (Running Node app via PM2 on port 80) about running the server on port 5000 and forwarding requests from port 80 to 5000. However, I want to understand why pm2 won't work on port 80.

Because PM2 will invoke Node without sudo. Yes, PM2 will run under root but that does not necessarily mean the actual Node process will run under root also.

Related

How to fix Kotlin Reactjs tutorial error: listen EADDRINUSE

As part of this basic tutorial, I downloaded the code from this repo, and attempted to run it with the command ./gradlew run on an Ubuntu 20.04 environment.
This resulted in the following error:
> Task :browserDevelopmentRun
✖ 「wds」: Error: listen EADDRINUSE: address already in use 127.0.0.1:8080
✖ 「wds」: Error: listen EADDRINUSE: address already in use 127.0.0.1:8080
at Server.setupListenHandle [as _listen2] (net.js:1318:16)
at listenInCluster (net.js:1366:12)
at GetAddrInfoReqWrap.doListen [as callback] (net.js:1503:7)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:69:8) {
code: 'EADDRINUSE',
errno: -98,
syscall: 'listen',
address: '127.0.0.1',
port: 8080
}
The process seems to be the only one running on this port, and attempts to get rid of any other processes using port 8080 (sudo kill -9 'sudo lsof -t -i:8080', killall node, etc...) have no effect. The conflicting processes seem to both come from within the tutorial web app.
Changing the port using a .env file also did not work. No matter what port is set (8081, 3000, 83000, etc...), there is still the error of port 8080 already being in use.
Does anyone know a solution to this issue? Thanks!
So while I didn't figure out exactly which dependency was causing this issue, on trying out a couple of the pull request on the repository, I found that this one: https://github.com/kotlin-hands-on/web-app-react-kotlin-js-gradle/pull/11 fixes the problem. Hopefully, it will be merged into the main branch soon!

Running simple node js server

I am running a simple node js server on my Mac. I also have mamp installed. Just FYI.I am able to go to localhost:8888 just fine and preview the web page. However, in my server.js file, if I include the host as a parameter to the server.listen function like so
server.listen(127.0.0.1, 8888, function(){
console.log('Server running');
})
I get the following error in my terminal.
Error: listen EADDRINUSE 127.0.0.1
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at Server.setupListenHandle [as _listen2] (net.js:1338:19)
at listenInCluster (net.js:1396:12)
at Server.listen (net.js:1491:5)
at Object.<anonymous> (/Users/username/Desktop/nodeServer/server.js:12:8)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
Users-Mac-Pro:nodeServer user$
If I just have
server.listen(8888, function(){
console.log('Server running');
})
everything works fine. Does this have anything to do with the fact that I have mamp installed? Just want to understand why one works and the other doesn't.
Try putting your host after port as following
server.listen(8888, '127.0.0.1', function() {
console.log('Server running');
})
app.listen documentation
app.listen([port[, host[, backlog]]][, callback])
Some other process might have been using 8888 port. You can either kill that process or use someother port for your nodejs server.
To get the process id of the process using port 8888, use
lsof -i tcp:8888
and kill the process using
kill -9 <pid>
Hope this helps.
Probably you're currently running your server in that port. Try with:
killall node

port 100 not running in node.js in localhost

I have created a http server in node.js with the following code, and trying to run it on port 100:
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end("Howdy");
}).listen(100);
console.log("server running on port 100");
With this, the server does not start and I am getting the following error message on the linux console:
events.js:72
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:1135:5)
at Object.<anonymous> (/home/badhai/Desktop/mainn.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)
But if I lift a sails.js app on port 100, it runs successfully on port 100. However, the above code runs successfully on port 8081. I want to know if I need to make any changes in the server creation method or elsewhere so that it can be made to run successfully on port 100?
The EACCES part of the error message is the key here - it means you don't have access to that port. Ports < 1024 are system reserved. It's better to use ports in the range 1024-65535
Most modern operating systems limit binding to reserved ports (less than 1024) to processes running as root (or equivalent).
If you absolutely must bind to port 100, googling around will give you a bunch of ways to do it:
https://gist.github.com/firstdoit/6389682
It's better to use ports > 1024. I'm using ports starting from 3000. But if you really want to start it on port 100 and you understand what you're doing then install setcap and just allow to bind ports <1024.
> sudo apt-get install setcap
> sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/node
NodeJS can be installed in other directory also, so better to check where it is and call above setcap command with your path.
> which node
/usr/local/bin/node

New to using NodeJS Express Server

I trying to view a static html page at localhost, but get the following error message:
events.js:72
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:1135:5)
at Function.app.listen (/home/phil/xProgramming/AngJS/Project/node_modules/express/lib/application.js:536:24)
at Object.<anonymous> (/home/phil/xProgramming/AngJS/Project/server.js:6:5)
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)
I've tried killall, but I still get the same error message.
I new to Linux and am using Ubuntu 14.04
The EACCES error normally means that you are trying to bind to a port and failing. This is probably because you are trying to bind to port 80 without root permissions. Try running node with root permissions and it should work.
sudo node server.js
or
sudo nodejs server.js
Depending on how you have Ubuntu setup.
If this is a development environment instead of running as root you should use a port greater than 1024. Typically you will find people using port 3000 for node though 8000 and 8080 are also used. Here is how I normally do this when using Express.
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log("Listening on " + port);
});

ECONNREFUSED error while running express code

I am not able to run the server ...... I am getting the error as ECONNREFUSED
How to resolve this Error !
When i tried to use different ports .... all are giving me the same error !
ubuntu#ip-MyIP:~/rainmelon/projects/FindMyBuffet$ node app.js
Express server listening on port 7005
Error: connect ECONNREFUSED
at errnoException (net.js:884:11)
at Object.afterConnect [as oncomplete] (net.js:875:19)
--------------------
at Handshake.Sequence (/home/ubuntu/rainmelon/projects/FindMyBuffet/node_modules/mysql/lib/protocol/sequences/Sequence.js:15:20)
at new Handshake (/home/ubuntu/rainmelon/projects/FindMyBuffet/node_modules/mysql/lib/protocol/sequences/Handshake.js:9:12)
at Protocol.handshake (/home/ubuntu/rainmelon/projects/FindMyBuffet/node_modules/mysql/lib/protocol/Protocol.js:42:50)
at Connection.connect (/home/ubuntu/rainmelon/projects/FindMyBuffet/node_modules/mysql/lib/Connection.js:73:18)
at Object.<anonymous> (/home/ubuntu/rainmelon/projects/FindMyBuffet/app.js:15:12)
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)
The traceback says where the exception is coming from:
Error: connect ECONNREFUSED
at errnoException (net.js:884:11)
at Object.afterConnect [as oncomplete] (net.js:875:19)
--------------------
...
at Connection.connect (.../node_modules/mysql/lib/Connection.js:73:18)
--> ^^^^^
at Object.<anonymous> (/home/ubuntu/rainmelon/projects/FindMyBuffet/app.js:15:12)
--> ^^^^^^^^^
So your app can't connect to MySQL.
This is usually down to either incorrect hostname/portname in your MySQL driver configuration, the MySQL server not running, or that your MySQL server isn't configured to listen on TCP sockets. See here.
Your mysql process is down, which means it is not running. You need to restart your mysql process (changing ports wont help).
To solve this problem you need to restart it. You can do by doing any of the following:
You can start your wamp or xamp server which will automatically start the process.
Or you can open the commandline prompt and start it manually like so "c:\wamp\bin\mysql\mysql5.5.24\bin\mysqld.exe"
Note that using the second method will require knowing the exact location of your wamp folder like I have used mine up top.(in quotes)
You may do a netstat to find out the pid of process running on the port 7005 and then go for a forceful kill with the pid got.
like
netstat -plten |grep 7005
kill -9 16085
where 16085 is the pid obtained from prev command. And restart the express app.
reference
How to kill a process running on particular port in Linux?

Resources