Created
September 15, 2011 12:22
-
-
Save jinwei233/1219101 to your computer and use it in GitHub Desktop.
检查host是否可用
This file contains 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
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