58787

heroku app crash when listening on process.env.PORT

Question:

When i deploy this on heroku :

app.js

var port = process.env.PORT || 8080; app.listen(port, function(){ console.log("Listening on port : " + port); });

I get the 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 on port 8080 it work, and i cant understand why :

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

I have just started using nodejs and I don't really understand why it crashes.

Could someone either explain the cause of this or how to "debug" this?

Thanks !

Answer1:

OK, the problem was that express-generator create a /bin/www file that contain :

#!/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); });

Thats why the port was already used.

Thanks

Answer2:

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

I guess that nothing is listening to 8080 on your machine, this is why it works when you set it directly.

Use console.log(process.env.PORT) to print the port that you're trying to listen to. Then check what program is listening to this port using netstat -tunap, you should receive 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 relevant program and try again.

To debug nodejs I would recommend <a href="https://github.com/node-inspector/node-inspector" rel="nofollow">node-inspector</a>, very easy to use and it uses chrome developer tools ui.

Recommend

  • react-native-video [android] undefined is not an object (evaluating NativeModuels.UIManager.RCTVideo
  • Openshift node app failed to start
  • NodeJS - Disable JSONP on specific routes
  • Express.js : POST data as KEY of a req.body object instead of VALUE of req.body?
  • SmtpClient sends email to junk
  • Microsoft Chart Controls for Microsoft .NET Framework 4.0
  • Generating random numbers directly inside a .htaccess file
  • Can't connect Entity Framework to local SQL Server Express
  • ASP.NET MVC2 Error: No parameterless constructor defined for this object
  • Grunt watch Running “watch” task Waiting
  • UML diagram generator in Visual Studio 2010
  • Save website uploads in a subdomain
  • 3.0.0.M1: SSL - Invalid keystore format
  • Msvcr71.dll Msvcp71.dll missing
  • Change device language on Android 6.0 (Android M)
  • Relative paths. baseUrl and paths not working on ionic2 - angular2
  • How to log in to an HTTPS website with Jsoup?
  • How to get latest version of a artifact on Bintray using JSONP
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • how does System.Web.HttpRequest::PathInfo work?
  • error importing numpy
  • Swing - Get new component under mouseReleased
  • Very simple C++ DLL that can be called from .net
  • How to clear text inside text field when radio button is select
  • Launch Runnable Jar from Web Start
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • req.body is undefined - nodejs
  • C# - Serializing and deserializing static member
  • Get object from AWS S3 as a stream
  • Cross-Platform Protobuf Serialization
  • Sending data from AppleScript to FileMaker records
  • Validaiting emails with Net.Mail MailAddress
  • Do I've to free mysql result after storing it?
  • How to include full .NET prerequisite for Wix Burn installer
  • Is it possible to post an object from jquery to bottle.py?
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?