forked from godgunman/fanschecker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
115 lines (95 loc) · 2.94 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* Module dependencies.
*/
require('newrelic');
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var moment = require('moment-timezone');
var request = require('request');
var mongoose = require('mongoose');
var Fans = require('./store/models').Fans;
process.env.TZ = 'Asia/Taipei';
// Here we find an appropriate database to connect to, defaulting to
// localhost if we don't find one.
var uristring =
process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/fanschecker';
// Makes connection asynchronously. Mongoose will queue up database
// operations and release them when the connection is complete.
mongoose.connect(uristring, function (err, res) {
if (err) {
console.log ('ERROR connecting to: ' + uristring + '. ' + err);
} else {
console.log ('Succeeded connected to: ' + uristring);
}
});
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', function(req, res) {
res.render('index');
});
app.get('/chart', function(req, res) {
res.render('chart');
});
app.get('/api/fans', function(req, res) {
var skip = req.query.skip ;
var limit = req.query.limit;
var start = new Date(req.query.start);
var end = new Date(req.query.end);
console.log(start, end);
if (!skip) skip = 0;
if (!limit) limit = 1000;
var query = {};
var dateFilter = {};
if (start && start != 'Invalid Date') {
dateFilter['$gte'] = start;
}
if (end && end != 'Invalid Date') {
dateFilter['$lt'] = end;
}
if (dateFilter['$get'] || dateFilter['$lt'])
query['createdAt'] = dateFilter;
Fans
.find(query)
.sort('-createdAt')
.skip(skip)
.limit(limit)
.exec(function(err, result) {
var previous = undefined;
if (result.length ) {
var date = moment(result[result.length - 1].createdAt).tz('Asia/Taipei').format();
previous = req.protocol + '://' + req.get('host') + '/api/fans?limit=' + limit + '&end=' + encodeURIComponent(date);
}
res.json({
options: {
start: req.query.start,
end: req.query.end,
skip: skip,
limit: limit,
},
previous: previous,
size: result.length,
result: result,
});
});
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});