I try to increment a port number based on 8000 if the port is busy with EADDRINUSE
, and I thought it's as easy as the below:
var tryToOpenServer = function(port)
{
console.log('trying to Open: ' + port);
HTTPserver
.listen(port, function()
{
console.log('HTTP listening:' + port);
})
.on('error', function(err)
{
if (err.code === 'EADDRINUSE')
{
// port is currently in use
console.log('server Open error:' + port);
tryToOpenServer(port + 1);
}
});
};
tryToOpenServer(8000);
If the port:8000 is busy and got err.code
=== 'EADDRINUSE', the port is incremented and the code tries the port. The problem is that
.listen(port, function()
{
console.log('HTTP listening:' + port);
})
The above code somehow successfully runs on port:8000 even it throws an 'EADDRINUSE', so I've got 2 notifications: HTTP 8000 and 8001.
What do I miss? Please let me know thanks.
killall node
? – Behl