-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
63 lines (51 loc) · 1.6 KB
/
server.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
const express = require('express');
const app = express();
var ttt = require('dotenv').config()
app.set('port', (process.env.PORT || 3001));
// Express only serves static assets in production
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
const app_conf = {
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,
timeout_ms: 60*1000, // optional HTTP request timeout to apply to all requests.
}
const Twit = require('twit');
const T = new Twit(app_conf);
app.get('/api/tweets', function(req, res) {
const param = req.query.q;
const max_id = req.query.max_id;
if ((!param) && (!max_id)) {
res.json({
error: 'Missing required parameter `q` and `max_id`',
});
} else if (!max_id) {
searchTweets(param, res);
} else {
fetchMoreTweets(param, max_id, res);
}
});
function searchTweets(param, res) {
T.get('search/tweets', { q: "#" + param, count: 20 }, function(err, reply) {
res.json(reply);
})
}
function fetchMoreTweets(param, max_id, res) {
T.get('search/tweets', { q: param, max_id: max_id, include_entities: 1, count: 20 }, function(err, reply) {
res.json(reply);
})
}
app.get('/api/trends', function(req, res) {
searchTrends(res);
});
function searchTrends(res) {
T.get('trends/place', { id: 1 }, function(err, reply) {
res.json(reply);
})
}
app.listen(app.get('port'), () => {
console.log(`Find the server at: http://localhost:${app.get('port')}/`); // eslint-disable-line no-console
});