-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
53 lines (46 loc) · 1.36 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
'use strict';
const bodyParser = require('body-parser');
const env = require('node-env-file');
const express = require('express');
const httpsRedirect = require('express-https-redirect');
const logger = require('heroku-logger')
const morgan = require('morgan');
const session = require('express-session');
env(__dirname + '/.env', {raise: false});
const db = require('./db');
const passport = require('./passport');
const routes = require('./routes');
const app = express();
app.set('port', (process.env.PORT || 5000));
app.set('view engine', 'pug');
app.set('json spaces', 2);
if (process.env.HTTPS_REDIRECT === 'true') {
app.use(httpsRedirect());
}
app.use(express.static('static'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({
resave: false,
saveUninitialized: true,
secret: process.env.SESSION_SECRET,
}));
app.use(passport.initialize());
app.use(passport.session());
morgan.token('uid', (req, res) => req.user ? req.user.id : null);
app.use(morgan(':method :url :status :response-time ms :uid'));
app.use((req, res, next) => {
if (req.user) {
res.locals.user = req.user;
}
next();
})
app.use(routes);
app.locals.moment = require('moment');
db
.authenticate()
.then(() => {
logger.info('Connected to database.');
app.listen(app.get('port'), () => {
logger.info(`App is running on port ${app.get('port')}.`);
});
});