-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
88 lines (74 loc) · 2.2 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// import config from .env file
require('dotenv').config();
//module dependencies
const express = require('express'),
bodyparser = require('body-parser'),
helmet = require('helmet'),
twit = require('twit'),
app = express();
app.use(helmet());
app.use(bodyparser.urlencoded({ extended: false }));
/* storing API key variables to be accessed via dotenv, as
** specified above
*/
var TwitterAccess = new twit({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
});
app.get('/', function(req, res) {
// validations on input
const processedInput = processUserInput(req.query);
let twitter_response = [];
function findTweets() {
TwitterAccess.get(
'search/tweets',
{
q: ['the', 'a', 'an', processedInput.searchTerm, 'exclude:retweets'],
count: processedInput.request_count,
},
function(err, data, response) {
if (err) {
twitter_response = err;
}
twitter_response = data.statuses.map(function(tweet) {
return tweet.text;
});
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
res.setHeader('Content-Type', 'application/json');
res.json({ tweetResults: twitter_response });
},
);
}
findTweets();
});
function processUserInput(userInput) {
/* check for a search term in the query, and
** if there isn't one, then just substitute
** 'awesome sauce'.
** Also keep string to a reasonable length.
*/
let searchTerm = userInput.term || 'awesome sauce';
if (searchTerm.length > 50) {
searchTerm = searchTerm.substring(0, 50);
}
// ask for 1 tweet by default if no count sent
let request_count = parseInt(userInput.count, 10) || 1;
// ask for 1 tweet if the count is something wacky
if (isNaN(request_count)) {
request_count = 1;
}
// ask for 100 tweets at most
if (request_count > 100) {
request_count = 100;
}
const processedInput = {
searchTerm,
request_count,
};
return processedInput;
}
const port = process.env.PORT || 3001;
console.log('app listening at ' + port);
app.listen(port);