I am creating a Node web app using Express.js and twitter.js. I am sucsessfully recieving the twitter stream on my server, however, I am unable to stream that data to my client(AJAX call). When attempting to do so I always recieve this error on the second tweet :
Error: Can't set headers after they are sent.
I know what causes this error (res.send
calls res.end
), but I am not sure how to get around it using Express.
HERE IS MY EXPRESS SERVER
app.get('/gamepage/twitter', function(req, res, next){
client.stream('statuses/filter.json', params, function(stream){
stream.on('data', function(event){
console.log(event);
//game module calls
mix.getTweets(event.user.name, event.text); //Store tweet
mix.getLastPrice(); // set the last price
let last = mix.lastPrice;
console.log('last price: '+ last);
//mix.trackScores(mix.tweets.user); // look for cmd and change score accordingly
// send object
res.send({
allTweets: mix.tweets,
price: mix.price,
scores: mix.scores
});
});
stream.on('error', function(error){
throw error;
});
HERE IS MY AJAX
function getTweets () {
let xhrTweets = new XMLHttpRequest();
xhrTweets.open('GET', 'http://localhost:3003/gamepage/twitter', true);
xhrTweets.addEventListener('load', function(){
console.log('loaded');
//getPrice();
let data = JSON.parse(xhrTweets.response);
let mostRecent = 0//data.allTweets.length-1;
console.log(data);
console.log(data.allTweets);
console.log(data.price);
console.log(data.scores);
let oldContainer = document.getElementById('gameresults');
let newContainer = document.createElement('div');
newContainer.setAttribute('id', 'gameresults');
document.getElementById('resultcontainer').replaceChild(newContainer, oldContainer);
let resultDisplay = document.createElement('div');
resultDisplay.setAttribute('class', 'tweetContainer');
let newTweet = document.createElement('p');
let tweetUser = document.createElement('p');
let newPrice = document.createElement('p');
let displayText = document.createTextNode(data.allTweets[mostRecent].text);
let displayUser = document.createTextNode(data.allTweets[mostRecent].user);
let displayPrice = document.createTextNode(data.price);
newContainer.appendChild(resultDisplay);
resultDisplay.appendChild(tweetUser);
resultDisplay.appendChild(newTweet);
resultDisplay.appendChild(newPrice);
newTweet.appendChild(displayText);
tweetUser.appendChild(displayUser);
newPrice.appendChild(displayPrice);
});
//xhrTweets.timeout = 15*1000;
//xhrTweets.addEventListener('timeout', function(data){
// console.log('timeout');
// console.error(data);
//});
xhrTweets.send();
}
So how do I stream data from twitter Through express and into my AJAX call while keeping the stream open for later data?