2017-04-02 4 views
0

私はNodeJSをかなり新しくしています。私は自分のノードサーバーがWebページを更新し続けるようにする方法を知りました。NodeJSを使用してウェブページを更新し続けるには?

私がやっているのは、私がtwitterからつぶやきを取っているということです。今はすべてを基本的なWebページに表示したいだけです。最初の実行中は、次のようになります。

名前:ap。テキスト:RT @APCentralRegion:Yevgeny Yevtushenko、ロシアの詩人 戦争の残虐行為に焦点を当て、オクラホマで死ぬ。 名前:ap。テキスト: の数百人がロシアの反政府抗議に出た後、警察の逮捕 は無許可の集会で数十回。 ...

しかし、私は次のエラーメッセージを取得分程度の時間間隔の後:私は内部のつぶやき機能を持っていたとき

response.write("*********End of One Interval*********") 
       ^
TypeError: Cannot read property 'write' of undefined 

私のコードは以下であることを心に留めておきます私のcreateServerが、それはまだそれが適切にページを更新するために、失敗のと同じ挙動を示した、しかし、ターミナルウィンドウは常にはconsole.log使用して正しく更新されました

あなたはargumを渡す必要が
// 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); 

答えて

1

コールバックに入る。以下を実行してください。

setInterval(function(){ 
     favoriteTweet(response,a,b) 
}, 60000); 
+0

このような簡単な解決策は、ありがとうございます。私はJavaScriptでコールバックを忘れています。 – SomeStudent

関連する問題