-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
137 lines (122 loc) · 3.85 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var CONFIG = require('./public/js/config.js');
var http = require('http');
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var assert = require('assert');
var MongoClient = require('mongodb').MongoClient;
var url = CONFIG.DB_URL + CONFIG.DB_NAME;
/*
EXPRESS APP SETUP
*/
var app = express();
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
/*
INDEX ROUTE
*/
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
/*
ABOUT ROUTE
*/
app.get('/about', function (req, res) {
res.sendFile(__dirname + '/about.html');
});
/*
CHANNELS POST ENDPOINT
*/
app.post('/channels', function (req, res) {
MongoClient.connect(url, function(err, db) {
//assert.equal(null, err);
if (!err) {
console.log('Connected correctly to server to find channels');
var collection = db.collection('channels');
collection.find({}).toArray(function(err, result) {
getEntries(result, res, req.body);
db.close();
});
} else {
res.json( {success: false } );
}
});
});
/*
CREATE EXPRESS SERVER
*/
http.createServer(app).listen(app.get('port'), process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1' , function(){
console.log('Express server listening on port ' + app.get('port'));
});
/**
* Gets all entries from a time period that match word1 and word2
* @param {object} channels
* @param {object} res response
* @param {object} rbody request body
*/
function getEntries (channels, res, rbody) {
MongoClient.connect(url, function(err, db) {
//assert.equal(null, err);
if (!err) {
console.log('Connected correctly to server to find entries');
var collection = db.collection('entries');
// If search for single day make it search from 00:00 to 00:00
if ( rbody.date_from == rbody.date_to ) {
var pub_query = {
$gte: new Date(rbody.date_from),
$lte: new Date(new Date(rbody.date_from).setHours(24))
};
} else {
var pub_query = {
$gte: new Date(rbody.date_from),
$lte: new Date(rbody.date_to)
};
}
collection.find({
pub_date: {
$gte: new Date(rbody.date_from),
$lte: new Date(new Date(rbody.date_to).setHours(24))
},
$where: function() { return (obj.title.toLowerCase().indexOf(rbody.word1.toLowerCase()) != -1 || obj.title.toLowerCase().indexOf.indexOf(rbody.word2.toLowerCase()) != -1 ) }
} ).sort({pub_date: -1}).toArray(function(err, result) {
stitchQuery(channels, result, rbody, db, res);
});
} else {
res.json( {success: false } );
db.close();
}
});
};
/**
* Constructs object from entries to serve back
* @param {object} channels channels object from db
* @param {object} entries entries object from db
* @param {object} rbody request body
* @param {object} db database connection
* @param {object} res response object
* @return {object} [description]
*/
function stitchQuery (channels, entries, rbody, db, res) {
channels.forEach( function(c, i) {
channels[i]['word1'] = [];
channels[i]['word2'] = [];
if ( entries ) {
// Push word to respective channel
entries.forEach( function(r, ind){
if ( r.title.toLowerCase().indexOf(rbody.word1.toLowerCase()) != -1 && r.source == channels[i]._id ) {
channels[i]['word1'].push(r);
}
if ( r.title.toLowerCase().indexOf(rbody.word2.toLowerCase()) != -1 && r.source == channels[i]._id ) {
channels[i]['word2'].push(r);
}
});
}
});
console.log('Close DB Connection');
res.json( {success: true, results: channels} );
db.close();
};