Nucktrooper Nucktrooper - 1 year ago 221
Node.js Question

Error EADDRNOTAVAIL with Heroku nodejs server

I have developped a nodejs server on OpenShift and I am now trying to make the same kind of server on Heroku for a new project.

Here is a minimal code of my server:

var http = require('http');
var port = process.env.PORT || 8080;
var address = process.env.IP || '';


var server = http.createServer(function(req, res)
res.writeHead(200, { 'Content-Type': 'text/html', 'Access-Control-Allow-Origin': '*' });
res.write(JSON.stringify({ valid: true }));

server.listen(port, address);

Differences with my OpenShift server are:

  • The replacement of variables process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP by process.env.PORT and process.env.IP.

  • I set the IP variable with this command line: heroku config:set

I cannot start my server, it always crash and I don't understand why, the logs of my server are:
throw er; // Unhandled 'error' event
at Object.exports._errnoException (util.js:893:11)
at exports._exceptionWithHostPort (util.js:916:20)
at Server.__dirname.Server.Server._listen2 (net.js:1233:19)
at net.js:1391:9
at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:63:16)
at listen (net.js:1282:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:82:10)

Answer Source

My guess would be that your Heroku server will not have your (external) server address as its (internal) IP-number (the external IP-address will most likely terminate earlier in the Heroku network stack), which means that you cannot explicitly listen on it (which is basically what EADDRNOTAVAIL means).

Instead, don't use an address to listen to at all:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download