Anubhav Sahu Anubhav Sahu - 5 months ago 67
Node.js Question

i was deploying my nodejs app on evennode but i am getting this error

i have been trying everything but it keeps throwing this error in the log. and says 502 bad gateway when trying to access the url.

events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at Server._listen2 (net.js:1221:19)
at listen (net.js:1270:10)
at net.js:1379:9
at nextTickCallbackWith3Args (node.js:453:9)
at process._tickCallback (node.js:359:17)
at Function.Module.runMain (module.js:443:11)
at startup (node.js:139:18)
at node.js:968:3


here's my app.js (also my start script as specified in package.json).

var app = require('express')();
var http = require('http').Server(app);
var path = require('path');
var io = require('socket.io')(http);


var routes = require('./routes/home');
var users = require('./routes/users');

io.on('connection', function(socket) {
console.log('a user connected');
socket.on('message', function (data) {
console.log(data);
io.emit('msg', data);
});
});

http.listen(process.env.port, '0.0.0.0', function(err) {
console.log('server runninng at ' + http.url );
});


although at console(stdout) it says
-> starting app
-> running server at undefined

Answer

You should update the process.env.port to process.env.PORT since env vars are case sensitive.

The listen should look like this:

http.listen(process.env.PORT, '0.0.0.0', function(err) {
    console.log('server runninng at ' + http.url );
});