-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (60 loc) · 1.76 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
'use strict';
var bookshelf = require('./bookshelf');
var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var session = require('express-session');
var passport = require('passport');
var channel = require('./channel');
var cors = require('cors');
var auth = require('./auth');
var api = require('./api');
var KnexStore = require('connect-session-knex')(session);
var app = module.exports = express();
app.set('trust proxy', 1);
// heroku already logs every incoming request
// we don't want log messages during tests
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cors({origin: true, credentials: true}));
app.use(session({
store: new KnexStore({knex: bookshelf.knex, tablename: 'user_sessions'}),
secret: process.env.SECRET,
cookie: {
secure: process.env.NODE_ENV === 'production',
httpOnly: true,
maxAge: 30 * 24 * 60 * 60 * 1000 // 30 days
},
saveUninitialized: false,
resave: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.use('/sse', connectChannel);
app.use('/auth', auth);
app.use('/api', api);
if (process.env.NODE_ENV !== 'test') {
app.use(function(err, req, res, next) {
console.error(err.stack);
if (req.user && req.user.isAdmin()) {
res.status(500).send(err.stack);
} else {
res.sendStatus(500);
}
});
} else {
app.use(function(err, req, res, next) {
process.nextTick(function() {
throw err;
});
});
}
app.use(function(req, res) {
res.sendStatus(404);
});
function connectChannel(req, res) {
channel.addClient(req, res);
}