Last active
December 3, 2018 08:43
-
-
Save ansarisufiyan777/6fe272482983fa5bbb409f3051eebf52 to your computer and use it in GitHub Desktop.
Check server status (Node)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//npm install bluebird | |
//npm install net | |
var net = require('net'); | |
var Promise = require('bluebird'); | |
function checkConnection(host, port, timeout) { | |
return new Promise(function(resolve, reject) { | |
timeout = timeout || 10000; // default of 10 seconds | |
var timer = setTimeout(function() { | |
reject("timeout"); | |
socket.end(); | |
}, timeout); | |
var socket = net.createConnection(port, host, function() { | |
clearTimeout(timer); | |
resolve(); | |
socket.end(); | |
}); | |
socket.on('error', function(err) { | |
clearTimeout(timer); | |
reject(err); | |
}); | |
}); | |
} | |
checkConnection("localhost", 4200).then(function() { | |
// successful | |
console.log("Server is running"); | |
}, function(err) { | |
console.log("Server is not running"); | |
// error | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment