0
我對NodeJS相當陌生,想知道如何讓我的節點服務器不斷更新網頁。如何使用NodeJS更新網頁?
所以我正在做的是我從Twitter獲取推文,現在我只想在基本網頁上顯示它們。在第一次運行期間,它看起來像這樣:
名稱:ap。 Text:RT @APCentralRegion:俄羅斯詩人Yevgeny Yevtushenko 專注於戰爭暴行的人,死於俄克拉荷馬州 姓名:ap。文字: 一個星期後,數百人蔘加了在俄羅斯的反政府抗議活動,警方在未經授權的集會上逮捕了幾十人。 ...
然而,約一分鐘,我得到了以下錯誤消息的時間間隔後:
response.write("*********End of One Interval*********")
^
TypeError: Cannot read property 'write' of undefined
我的代碼如下,請記住,當我裏面有鳴叫功能我createServer它仍然表明,它未能妥善更新頁面的相同的行爲,但是,在終端窗口總是正確更新使用的console.log
// Dependencies =========================
var
twit = require('twit'),
config = require('./config');
var http = require("http");
var url = require("url");
var Twitter = new twit(config);
// FAVORITE BOT====================
// Heavily modified from original tutorial.
var favoriteTweet = function(response, a, b){
var params = {
screen_name: a,
count: b,
// q: '#aprilfools, #aprilfoolsday', // REQUIRED
// result_type: 'recent', //recent tweets only.
//lang: 'en'
}
//console.log("Params: " + params.q);
// find the tweet
Twitter.get('statuses/user_timeline', params, function(err,data){
// find tweets
//console.log(data);
var tweet = data; //.statuses for actual tweets.
//console.log(tweet);
for(var result in tweet) {
response.write("Name: " + a + ". Text: " + tweet[result].text + "\n");
console.log("Resulting text: " + tweet[result].text);
console.log("Created At: " + tweet[result].created_at);
}
//console.log(tweet);
response.write("*********End of One Interval*********")
console.log("*********End of One Interval*********")
});
}
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type":"text/plain"});
var params = url.parse(request.url, true).query;
var a = params.name;
var b = parseInt(params.count);
// grab & 'favorite' as soon as program is running...
favoriteTweet(response, a, b);
// 'favorite' a tweet in every 1 minute
setInterval(favoriteTweet, 60000);
}).listen(9090);
這樣一個簡單的解決方案,非常感謝。我一直在JavaScript中忘記回調。 – SomeStudent