I am fairly new to Node.JS and I was wondering how I could have my node server keep updating the webpage. So what I am doing is that I am fetching tweets from twitter, and for now I just want to display them all on a basic webpage. During its first run it looks like this:
Name: ap. Text: RT @APCentralRegion: Yevgeny Yevtushenko, Russian poet who focused on war atrocities, dies in Oklahoma Name: ap. Text: A week after hundreds attended anti-government protests in Russia, police arrest dozens at unauthorized rallies. ...
However, after the time interval of about a minute I get the following error message is:
response.write("*********End of One Interval*********")
^
TypeError: Cannot read property 'write' of undefined
my code is below, keep in mind that when I had the tweet function inside my createServer it still showed the same behavior of it failing to properly update the page, however, the terminal window was always updated correctly using 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);
via SomeStudent
No comments:
Post a Comment