forked from forcedotcom/OrgMonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
68 lines (60 loc) · 1.83 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
/*
Copyright (c) 2017, salesforce.com, inc.
All rights reserved.
Licensed under the BSD 3-Clause license.
For full license text, see LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
const path = require('path')
const express = require('express')
const helmet = require('helmet')
const forcetls = require('force-ssl-heroku')
const compression = require('compression')
const passport = require('passport')
const session = require('express-session')
const bodyparser = require('body-parser')
const routes = require('./routes/index.js')
const db = require('./lib/db.js')
const serializeError = require('serialize-error')
/* Express Config */
let app = express()
app.use(helmet())
app.use(forcetls)
app.use(compression())
app.set('view engine', 'pug')
app.set('views', path.join(__dirname, '/views'))
app.set('json spaces', 2)
app.use('/public', express.static(path.join(__dirname, '/public')))
app.use(bodyparser.json()) // for parsing application/json
app.use(bodyparser.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
app.use(session({
name: 'sid',
secret: process.env.COOKIE_SECRET || 'change me',
proxy: true,
resave: true,
saveUninitialized: true,
cookie: {
path: '/',
httpOnly: true,
secure: true,
maxAge: 3600000 // 1 hour
}
}))
/* Set DB var */
app.set('db', db)
/* Passport */
require('./lib/passport')(passport)
app.use(passport.initialize())
app.use(passport.session())
/* Routes */
app.use('/', routes)
/* Start */
const port = process.env.PORT || 3000
app.listen(port, () => {
console.log(`App listening on port ${port}`)
})
/* Error handlers */
app.use((err, req, res, next) => {
let email = (req.user && req.user.email) || '[email protected]'
console.error(`[${email}] Unhandled application error`, serializeError(err))
res.status(500).send(err)
})