Skip to content

Instantly share code, notes, and snippets.

@jinwei233
Created September 15, 2011 12:22
Show Gist options
  • Save jinwei233/1219101 to your computer and use it in GitHub Desktop.
Save jinwei233/1219101 to your computer and use it in GitHub Desktop.
检查host是否可用
var options = {
host: 'www.google.com',
port: 80,
path: '/index.html'
};
http.get(options, function(res) {
if (res.statusCode == 200) {
console.log("success");
}
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
function testPort(port, host, cb) {
http.get({
host: host,
port: port
}, function(res) {
cb("success", res);
}).on("error", function(e) {
cb("failure", e);
});
}
For a tcp socket just use net.createConnection
function testPort(port, host, cb) {
net.createConnect(port, host).on("connect", function(e) {
cb("success", e);
}).on("error", function(e) {
cb("failure", e);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment