I have a working function that, when used in the app.js file, will successfully open a twitter stream and display the content below(if console.log is used instead of print). However, when I take the function and put it in a script inside index.html, I get the error “referenceError: require not defined” I also get a GET error with the other scripts..perhaps I am referencing them incorrectly, but I’ve tried including them with only one .
I am trying to learn all this on my own. I have made a simply static website in the past, but only using html, css, and some basic jquery. I want to be able to stylize the text output by the function with css.
<!DOCTYPE html>
<html>
<head>
<title>Streaming</title>
<script src="..Twitter/config.js"></script>
<script src="..Twitter/package.json"></script>
<script src="..Twitter/app.js"></script>
<script type = "text/javascript">
function displayStream() {
var Twit = require('twit');
var T = new Twit(config);
var stream = T.stream('statuses/filter', { track: 'Bernie Sanders' });
var count = 0;
var totalSentiment = 0;
stream.on('tweet', function (tweet) {
//Exclude retweets
if(tweet.text.startsWith("RT") === false) {
print(tweet.text)
print("Sentiment score: " + sentiment(tweet.text).score)
count += 1;
totalSentiment += sentiment(tweet.text).score;
print(count + " tweets analyzed");
print("Aggregate Sentiment: " + totalSentiment);
print("Average Sentiment:" + (totalSentiment/count).toFixed(2) + "n");
};
});
};
</head>
<body onload="displayStream();">
<h1>Welcome to my website</h1>
<p>displayStream()</p>
2
Answers
For just javascipt you can use fetchApi. It’s will provide same feel as AJAX does.
For example :
Thanks,
Dinesh