-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
75 lines (58 loc) · 2.15 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
64
65
66
67
68
69
70
71
72
73
74
75
'use strict';
try {
// Loads environment settings from '.env' into process.env
// This is for local development
require('dotenv').load();
} catch (e) {
console.log('.env file not found')
}
var express = require('express');
var Twitter = require('twitter');
var Apicache = require('apicache');
var cache = Apicache.middleware;
var TwitterStreamingService = require('./src/services/TwitterStreamingService.js');
var app = express();
/** Add proper headers for CORS support for an Express application */
function CORSHeaders(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Authorization, Accept, *");
next();
}
/** enable CORS support */
app.use(CORSHeaders);
/** health check route */
app.get('/health', function (req, res) {
res.status(200).send('ok');
});
/** twitter api proxy */
app.get('/api/twitter/1.1/*', cache('3 minutes'), function (req, res) {
var client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});
var params = req.query;
params.count = true;
client.get(req.params[0], params, function (error, tweets, response) {
res.status(response.statusCode).send(tweets);
});
});
/** Register API Middleware **/
/** VOTES **/
app.get('/api/v1/votes', require('./src/api/v1/votes/list.votes.js'));
app.get('/api/v1/votes/:billIdentifier', require('./src/api/v1/votes/get.votes.js'));
/** Start Twitter Streaming Api **/
TwitterStreamingService.startStream();
/** Static Files */
app.use('/', express.static(__dirname + '/src/public'));
/** This route deals enables HTML5Mode by forwarding missing files to the index.html */
app.get('/*', function (req, res) {
res.sendFile(__dirname + '/src/public/index.html')
});
var port = process.env.PORT || 8080;
var server = app.listen(port, function () {
console.log('listening on port: %s', port);
});
module.exports = app;