2016-06-28 26 views
3

我想看這個網址進行修改如何使用Node.js觀看URL?

http://redsismica.uprm.edu/Data/prsn/EarlyWarning/Catalogue.txt

這是更新的文本文件。我想用nodejs中的請求模塊來監視它的變化。

我該怎麼做?

+0

我無法回答你的問題,因爲它已經關閉了......但是隻是做一個'HEAD'請求並且將'ETag'和最後一個'ETag'進行比較,實際上,你所要做的是在本地緩存數據。這個URL的'Cache-Control'頭部表示你不應該緩存它,所以如果你這樣做的話,要注意這一點。 – Brad

回答

1

有許多方法可以做你想做的事情,所以我會告訴你使用request.js框架和簡單的Node.js setIntervalclearInterval這樣做的最簡單的方法。

var request = require('request'); 
var catalogue; 

var checkCatalogue = function() { 
    request('http://redsismica.uprm.edu/Data/prsn/EarlyWarning/Catalogue.txt', function (error, response, body) { 
     if (!error && response.statusCode == 200) { 
      if(!catalogue){ 
      catalogue = body; 
      } else { 
       // here you should do a complex verification, for example check line by line... 
       if(catalogue === body) { 
        console.log('everything is equal'); 
       } else { 
        console.log('something is different'); 
        // stop the interval to do something (if you needed) 
        clearInterval(checkCatalogue); 
        //... 
        // do what you want to do with the catalogue 
       } 

      } 
     } 
    }); 
} 

// execute checkCatalogue function once per second 
setInterval(checkCatalogue, 1000); 

這只是一個可以打開你的想法解決你的問題的例子。我無法測試我的代碼,因爲我的電腦中沒有安裝Node.js :( 但我的建議背後的想法是,您可以使用計時器功能來不時檢查目錄文件中更改的內容