-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
63 lines (51 loc) · 1.72 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
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
Twitter = require('node-tweet-stream');
require('dotenv').load();
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({extended: true}));
// connect to socket
io.on('connection', function (socket) {
console.log('user connected');
socket.on('disconnect', function() {
console.log('user disconnected');
});
});
// set up tweet stream
twitter = new Twitter({
consumer_key: process.env.TWITTER_KEY,
consumer_secret: process.env.TWITTER_SECRET,
token: process.env.TWITTER_TOKEN,
token_secret: process.env.TWITTER_TOKEN_SECRET
});
// stream tweets
twitter.on('tweet', function (tweet) {
console.log('tweet received', tweet);
io.sockets.emit('receive_tweet', tweet);
});
// root route automatically tracks tweets from `searchKey`
// default is 'San Francisco'
app.get('/', function (req, res) {
twitter.untrack(req.searchKey);
console.log('untracking', req.searchKey);
req.searchKey = 'San Francisco';
twitter.track(req.searchKey);
console.log('tracking', req.searchKey);
res.render('site/index', { searchKey: req.searchKey });
});
// when user searches new keyword, update `searchKey`
app.post('/search', function (req, res) {
twitter.untrack(req.searchKey);
console.log('untracking', req.searchKey);
req.searchKey = req.body.keyword;
twitter.track(req.searchKey);
console.log('tracking', req.searchKey);
res.render('site/index', { searchKey: req.searchKey });
});
server.listen(process.env.PORT || 3000, function() {
console.log('server started');
});