I'm trying to deploy a node server to heroku and I'm getting this error: throw er; // Unhandled 'error' event - node.js

I'm trying to deploy a node app to heroku and it looks like judging from other questions that heroku is dynamically assigning a port and it is somehow incompatible with something.
(I had to remove some details to post the question)
events.js:182
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Object._errnoException (util.js:1041:11)
at _exceptionWithHostPort (util.js:1064:20)
at Server.setupListenHandle [as _listen2] (net.js:1305:19)
at listenInCluster (net.js:1370:12)
at Server.listen (net.js:1466:7)
at Function.listen (/app/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/app/index.js:9:21)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! TFP#1.0.0 start: `node index.js`
npm ERR! Failed at the TFP#1.0.0 start script.

Problem
I had the same issue. I ran into this problem when I tried to set the port manually:
app.listen(80, function () {
console.log('Example blog app listening on port 80!')
});
This led me to the same EACCES error.
Solution
I was able to solve the problem by going back and looking through the Heroku docs where I found:
The command in a web process type must bind to the port number specified in the PORT environment variable. If it does not, the dyno will not start.
So, assuming you're using express, if you switch the port assignment to look something like this:
app.set('port', (process.env.PORT || 5000));
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
Then you application should start properly.
Solution without Express.js
If you are not using Express, then you can set the port simply as:
var PORT = process.env.PORT || 5000;
Where process.env.PORT will be your production port, and 5000 will be the port you would use when testing your server locally.

I have faced this kind of problem. The exact error is the port 80 is already enabled in another project or app. So before start your app, stop the other app mapped to 80 port.
Now you can run your app without this error.
Thanks

Related

Node Server Running on Port 80 errors with using PM2

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.

why node js socket not working when change port to https?

Normally in app.js i use this code on port 3000 it's work good (on my-domain.com:3000).
.
http.listen(3000, function(){
console.log('start server on port :3000');
});
then i want to use on https, so i change app.js to
http.listen(443, function(){
console.log('start server on port :443');
});
When run node app.js , it's show error
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::443
at Object.exports._errnoException (util.js:1012:11)
at exports._exceptionWithHostPort (util.js:1035:20)
at Server._listen2 (net.js:1252:14)
at listen (net.js:1288:10)
at Server.listen (net.js:1384:5)
at Object.<anonymous> (/home/admin/web/my-domain.com/public_html/app.js:28:6)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
Normally i have to access to my-domain.com:3000 for user chat. So i want to know how can i access to https://www.my-domain.com for user chat ?
You have the following error message:
Error: listen EADDRINUSE :::443
[...]
This message means that the port 443 is currently in use by some process.
You can check which process is actually using the said port by executing one of the many tools for network checking (such as netstat for Windows, lsof or netstat on Linux).
Refer to the manual for those tools to achieve the correct result, based upon your operating system.

Why i can not use express node js on my server?

node js version node -v ===> v6.3.1 was install on my server.
then i will install express by this step
npm init
in entry point: (index.js)
npm install express --save
then create app.js
const express = require('express')
const app = express()
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(3000, () => console.log('Example app listening on port 3000!'))
and then node app.js
and it's show error
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3000
at Object.exports._errnoException (util.js:1012:11)
at exports._exceptionWithHostPort (util.js:1035:20)
at Server._listen2 (net.js:1252:14)
at listen (net.js:1288:10)
at Server.listen (net.js:1384:5)
at EventEmitter.listen (/home/admin/web/my-domain.com/public_html/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/home/admin/web/my-domain.com/public_html/app.js:6:5)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
how can i do ?
Choose a different port number as 3000 since some service is already bound to that port given that error.
Change it in the following line:
app.listen(3000, () => console.log('Example app listening on port 3000!'))
EADDRINUSE means that the port number which listen() tries to bind the server to is already in use.
So in this case, there must be running a service listening on port 3000 already.
you can run this command before start your app
sudo pkill -kill node // to kill any node app running then start your app

Error while running application through NPM node and ember

I am new to this whole npm node concept and currently on learning phase. I am trying to resolve an error that is not letting me perform the Event router for my web application in node. Here is the error below and do let me know what is the problem or issue and what steps to take to resolve it.
Important: use process.env.PORT as the port and process.env.IP as the host in your scripts!
Debugger listening on port 15454
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::8080
at Object.exports._errnoException (util.js:907:11)
at exports._exceptionWithHostPort (util.js:930:20)
at Server._listen2 (net.js:1250:14)
at listen (net.js:1286:10)
at Server.listen (net.js:1382:5)
at EventEmitter.listen (/home/ubuntu/workspace/nodeproject/node_modules/express/lib/application.js:617:24)
at Object.<anonymous> (/home/ubuntu/workspace/nodeproject/app.js:55:5)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
Error: listen EADDRINUSE :::8080
Some other process is already listening on port 8080. Either stop that process or change the port of the ember app.

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);
});

Resources