Nucktrooper Nucktrooper - 2 months ago 35
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 || '127.0.0.1';

console.log(address);
console.log(port);

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

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 IP=MYSERVERADDRESS.com



I cannot start my server, it always crash and I don't understand why, the logs of my server are:

MYSERVERADDRESS.com
41184
events.js:154
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL MYSERVERIP:41184
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

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:

server.listen(port);