È possibile implementare un httpWatcher (che imita il contratto di file watcher - fs.watch). Potrebbe eseguire il polling di un endpoint http (una route di stato o un file html) e generare una richiamata quando viene restituito un 200 (o quando è stata raggiunta una corsa massima). Qualcosa di simile a questo:
var request = require('request');
var watch = function(uri) {
var options;
var callback;
if ('object' == typeof arguments[1]) {
options = arguments[1];
callback = arguments[2];
} else {
options = {};
callback = arguments[1];
}
if (options.interval === undefined) options.interval = 2000;
if (options.maxRuns === undefined) options.maxRuns = 10;
var runCount = 0;
var intervalId = setInterval(function() {
runCount++;
if(runCount > options.maxRuns) {
clearInterval(intervalId);
callback(null, false);
}
request(uri, function (error, response, body) {
if (!error && response.statusCode == 200) {
clearInterval(intervalId);
callback(null, true);
}
});
}, options.interval);
}
Quindi utilizzare in questo modo:
watch('http://blah.asdfasdfasdfasdfasdfs.com/', function(err, isGood) {
if (!err) {
console.log(isGood);
}
});
O passare opzioni ...
watch('http://www.google.com/', {interval:1000,maxRuns:3},
function(err, isGood) {
if (!err) {
console.log(isGood);
}
});
fonte
2013-05-04 04:01:54
continuo a ricevere l'errore: "Got error: collegare ECONNREFUSED" anche quando sto usando sudo. – user220755
@ user220755 si sta utilizzando la porta 8080, quindi cambiare la porta 80 a 8080 nel exmaple specificato. E per il test dare path come '/' – user568109
, purtroppo, non ha funzionato. – user220755