-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
501 lines (377 loc) · 13.4 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 5);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {
module.exports = require("express");
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var mongoUri = exports.mongoUri = process.env.MONGO_CONNECTION || 'mongodb://localhost:27017/test';
var users = exports.users = ['1417526761868371', '112199445534552', '141801682544988', '1449728135277608', '138162499562220'];
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
var _express = __webpack_require__(0);
var _express2 = _interopRequireDefault(_express);
var _morgan = __webpack_require__(13);
var _morgan2 = _interopRequireDefault(_morgan);
var _cookieParser = __webpack_require__(10);
var _cookieParser2 = _interopRequireDefault(_cookieParser);
var _bodyParser = __webpack_require__(9);
var _bodyParser2 = _interopRequireDefault(_bodyParser);
var _config = __webpack_require__(1);
var _config2 = _interopRequireDefault(_config);
var _Database = __webpack_require__(6);
var _index = __webpack_require__(8);
var _index2 = _interopRequireDefault(_index);
var _fetchPostsData = __webpack_require__(7);
var _fetchPostsData2 = _interopRequireDefault(_fetchPostsData);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var app = (0, _express2.default)();
(0, _Database.initDatabase)(function () {
// app.listen(27017, () => {
// console.log('listening on 3000')
// })
});
// view engine setup
app.set('views', 'views');
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use((0, _morgan2.default)('dev'));
app.use(_bodyParser2.default.json());
app.use(_bodyParser2.default.urlencoded({ extended: false }));
app.use((0, _cookieParser2.default)());
app.use(_express2.default.static('public'));
app.use('/', _index2.default);
app.use('/api/fetchPostsData', _fetchPostsData2.default);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function (err, req, res) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
/***/ }),
/* 3 */
/***/ (function(module, exports) {
module.exports = require("debug");
/***/ }),
/* 4 */
/***/ (function(module, exports) {
module.exports = require("http");
/***/ }),
/* 5 */
/***/ (function(module, exports, __webpack_require__) {
// #!/usr/bin/env node
/**
* Module dependencies.
*/
var app = __webpack_require__(2);
var debug = __webpack_require__(3)('sapronovkd:server');
var http = __webpack_require__(4);
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
/***/ }),
/* 6 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.initDatabase = exports.db = undefined;
var _config = __webpack_require__(1);
var MongoClient = __webpack_require__(12).MongoClient;
var db = exports.db = void 0;
var initDatabase = exports.initDatabase = function initDatabase(callback) {
MongoClient.connect(_config.mongoUri, function (err, client) {
if (err) return console.log(err);
exports.db = db = client.db('facebookDataFetch');
// db.collection('posts').find().toArray(function(err, results) {
// console.log(results)
// // send HTML file populated with quotes here
// })
callback();
});
};
/***/ }),
/* 7 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _express = __webpack_require__(0);
var _express2 = _interopRequireDefault(_express);
var _fb = __webpack_require__(11);
var _fb2 = _interopRequireDefault(_fb);
var _Database = __webpack_require__(6);
var _config = __webpack_require__(1);
var _dns = __webpack_require__(15);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
__webpack_require__(14);
var router = _express2.default.Router();
_fb2.default.options({ timeout: 10000, accessToken: 'EAACEdEose0cBAE0Tx3bBQHr26wG1y2fqxjRHJBPW1YYyvnDJygPZAcLLoTXVXIpyNyjU1bkZAuIpKMaPamcBIGjx6EL1SrrWZCKugggiBvXfZAFJAxZBnQF6MA6EahN34F6OIB0YVs6JK8qiuAjrN3anBkEPX1vPbcMDWv7Wr4DsZCZAksEZB4ZB6Mu72KipmeJGToOuL0Hq9GQZDZD' });
/* GET users listing. */
router.get('/', function (req, res) {
var _this = this;
var sinceTimestamp = String(Date.now() - 86400 * 10000000).slice(0, -3);
if (_config.users) return Promise.all(_config.users.map(function (user) {
return new Promise(function (resolve, reject) {
_fb2.default.api(user + '/feed', { fields: ['comments.limit(1).summary(true)', 'likes.limit(1).summary(true)', 'created_time'], limit: '10', since: sinceTimestamp }, function (data) {
if (!data || data.error) {
console.log(!data ? 'error occurred' : res.error);
return;
}
console.log(data);
if (data && data.data) {
_Database.db.collection('posts').insertMany(data.data.map(function (dataItem) {
return _extends({}, dataItem, { _id: dataItem.id });
})).then(function (data) {
console.log('saved');
return resolve(data);
}).catch(function (error) {
if (error) {
console.log(error);
reject(error);
}
});
}
});
});
})).then(function () {
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(data) {
var allPosts;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return _Database.db.collection('posts').find().toArray();
case 2:
allPosts = _context.sent;
res.json({ status: 'success', data: allPosts });
case 4:
case 'end':
return _context.stop();
}
}
}, _callee, _this);
}));
return function (_x) {
return _ref.apply(this, arguments);
};
}()).catch(function () {
var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(error) {
var allPosts;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (error) console.log(error);
_context2.next = 3;
return _Database.db.collection('posts').find().toArray();
case 3:
allPosts = _context2.sent;
res.json({ status: 'success', data: allPosts });
case 5:
case 'end':
return _context2.stop();
}
}
}, _callee2, _this);
}));
return function (_x2) {
return _ref2.apply(this, arguments);
};
}());
});
module.exports = router;
/***/ }),
/* 8 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _express = __webpack_require__(0);
var _express2 = _interopRequireDefault(_express);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var router = _express2.default.Router();
/* GET home page. */
router.get('/', function (req, res) {
res.render('index', { title: 'Express' });
});
exports.default = router;
/***/ }),
/* 9 */
/***/ (function(module, exports) {
module.exports = require("body-parser");
/***/ }),
/* 10 */
/***/ (function(module, exports) {
module.exports = require("cookie-parser");
/***/ }),
/* 11 */
/***/ (function(module, exports) {
module.exports = require("fb");
/***/ }),
/* 12 */
/***/ (function(module, exports) {
module.exports = require("mongodb");
/***/ }),
/* 13 */
/***/ (function(module, exports) {
module.exports = require("morgan");
/***/ }),
/* 14 */
/***/ (function(module, exports) {
module.exports = require("babel-polyfill");
/***/ }),
/* 15 */
/***/ (function(module, exports) {
module.exports = require("dns");
/***/ })
/******/ ]);