-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (42 loc) · 1.52 KB
/
index.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
const express = require("express");
const app = express();
let { getToken, getTweets, filterTweets } = require("./twitter.js");
const util = require("util");
getToken = util.promisify(getToken);
getTweets = util.promisify(getTweets);
app.use(express.static("ticker"));
app.get("/data.json", (req, res) => {
getToken()
.then((bearerToken) => {
Promise.all([
getTweets(bearerToken, "newscientist"),
getTweets(bearerToken, "nature"),
getTweets(bearerToken, "ScienceMagazine"),
])
.then((result) => {
const mergedResults = [
...result[0],
...result[1],
...result[2],
];
const sortedTweets = mergedResults.sort((a, b) => {
return new Date(b.created_at) - new Date(a.created_at);
});
return sortedTweets;
})
.then((sortedTweets) => {
filteredTweets = filterTweets(sortedTweets);
res.json(filteredTweets);
})
.catch((err) =>
console.log("index.js, error in sorted tweets: ", err)
);
})
.catch((err) => {
console.log("index.js, error in getToken: ", err);
res.sendStatus(500);
});
});
app.listen(process.env.PORT || 8080, () =>
console.log("twitter ticker server running")
);