When starting the application process.env.PORT

When I deploy this to heroku:

app.js

var port = process.env.PORT || 8080;

app.listen(port, function(){
    console.log("Listening on port : " + port);
});

      

I am getting error logs:

2014-11-08T18:05:34.300115+00:00 app[web.1]: events.js:72
2014-11-08T18:05:34.300402+00:00 app[web.1]:         throw er; // Unhandled 'error' event
2014-11-08T18:05:34.300404+00:00 app[web.1]:               ^
2014-11-08T18:05:34.303599+00:00 app[web.1]: Error: listen EADDRINUSE
2014-11-08T18:05:34.303605+00:00 app[web.1]:     at errnoException (net.js:904:11)
2014-11-08T18:05:34.303607+00:00 app[web.1]:     at Server._listen2 (net.js:1042:14)
2014-11-08T18:05:34.303608+00:00 app[web.1]:     at listen (net.js:1064:10)
2014-11-08T18:05:34.303610+00:00 app[web.1]:     at Server.listen (net.js:1138:5)
2014-11-08T18:05:34.303628+00:00 app[web.1]:     at Function.app.listen (/app/node_modules/express/lib/application.js:556:24)
2014-11-08T18:05:34.303629+00:00 app[web.1]:     at Object.<anonymous> (/app/bin/www:7:18)
2014-11-08T18:05:34.303630+00:00 app[web.1]:     at Module._compile (module.js:456:26)
2014-11-08T18:05:34.303632+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:474:10)
2014-11-08T18:05:34.303633+00:00 app[web.1]:     at Module.load (module.js:356:32)
2014-11-08T18:05:34.303635+00:00 app[web.1]:     at Function.Module._load (module.js:312:12)

      

but when I listen directly to port 8080 it works and I can't figure out why:

var port = 8080;
    app.listen(port, function(){
        console.log("Listening on port : " + port); //it work
    });

      

I just started using nodejs and I really don't understand why it is crashing.

Can anyone explain the reason for this or how to "debug" this?

Thank!

+3


source to share


2 answers


OK, the problem was that the express generator creates a file / bin / www containing:

#!/usr/bin/env node
var debug = require('debug')('toto');
var app = require('../app');

app.set('port', process.env.PORT || 3000);

var server = app.listen(app.get('port'), function() {
  debug('Express server listening on port ' + server.address().port);
});

      



This is why the port has already been used.

thank

+3


source


The "EADDRINUSE" error means that something is already listening on this port.

I am assuming the 8080 is not listening on anything on your computer, so it works when you install it directly.

Use the console.log(process.env.PORT)

port you are trying to listen on to print. Then check which program is listening on that port using netstat -tunap

, you should get something similar to this:



Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 0.0.0.0:37 0.0.0.0:* LISTEN 3425/inetd

tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN 3640/X

tcp 0 0 0.0.0.0:113 0.0.0.0:* LISTEN 3425/inetd

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 7586/sshd

      

Kill the corresponding program and try again.

To debug nodejs I would recommend node-inspector , it is very easy to use and uses chrome ui developer tools.

+1


source







All Articles