forked from ftlabs/inshort
/
index.js
72 lines (56 loc) · 1.79 KB
/
index.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
const dotenv = require('dotenv').config({ silent: process.env.NODE_ENVIRONMENT === 'production' });
const package = require('./package.json');
const debug = require('debug')(`${package.name}:index`);
const express = require('express');
const path = require('path');
const app = express();
const validateRequest = require('./helpers/check-token');
const articleSummary = require('./routes/article-summary');
const timeline = require('./routes/timeline');
const sentiment = require('./routes/sentiment');
const hbs = require('hbs');
// view engine setup
hbs.registerPartials(__dirname + '/views/partials');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
var requestLogger = function (req, res, next) {
debug("RECEIVED REQUEST:", req.method, req.url);
next(); // Passing the request to the next handler in the stack.
}
app.use(requestLogger);
// these routes do *not* have s3o
app.use('/static', express.static('static'));
app.use('/app', express.static('app'));
const TOKEN = process.env.TOKEN;
if (!TOKEN) {
throw new Error('ERROR: TOKEN not specified in env');
}
// these route *do* use s3o
app.set('json spaces', 2);
if (process.env.BYPASS_TOKEN !== 'true') {
app.use(validateRequest);
}
//Core Routes
app.use('/summary', articleSummary);
app.use('/timeline', timeline);
app.use('/sentiment', sentiment);
// ---
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname + '/app/dist/index.html'));
});
// app.use('/', (req, res) => {
// res.render('index');
// })
app.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).send('Something broke!')
})
//---
function startListening() {
app.listen(process.env.PORT, function () {
console.log('Server is listening on port', process.env.PORT);
});
}
//---
startListening();
module.exports = app;