Skip to content

Instantly share code, notes, and snippets.

@eplawless
Created April 18, 2016 07:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eplawless/ac41bc976f2982ecfe0f4253883339aa to your computer and use it in GitHub Desktop.
Save eplawless/ac41bc976f2982ecfe0f4253883339aa to your computer and use it in GitHub Desktop.
This had better not work...
var net = require('net');
function startServer(port, host, callback) {
var server = net.createServer();
server.listen(port, host, function() {
callback(undefined, server);
});
server.on('error', function(error) {
console.error('Ah damn!', error);
callback(error);
});
}
startServer(4000, '0.0.0.0', function(error, wildcardServer) {
if (error) return;
startServer(4000, '127.0.0.1', function(error, localhostServer) {
if (error) return;
console.log('Started both servers!');
});
});
@octaviotastico
Copy link

When I run this exact code on my computer, I get this error:

Ah damn! Error: listen EADDRINUSE: address already in use 127.0.0.1:4000
    at Server.setupListenHandle [as _listen2] (node:net:1319:16)
    at listenInCluster (node:net:1367:12)
    at doListen (node:net:1505:7)
    at processTicksAndRejections (node:internal/process/task_queues:84:21) {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '127.0.0.1',
  port: 4000
}

I cannot make them to run and print "Started both servers!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment