-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
106 lines (88 loc) · 2.82 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
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
'use strict'
// dependencies
const config = require('./config')
const express = require('express')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const http = require('http')
const path = require('path')
const passport = require('passport')
const mongoose = require('mongoose')
const helmet = require('helmet')
const cors = require('cors')
// var csrf = require('csurf')
// create express app
const app = express()
// keep reference to config
app.config = config
// setup the web server
app.server = http.createServer(app)
// setup mongoose
const mongoConnectSetting = {
useNewUrlParser: true,
useUnifiedTopology: true
}
mongoose.connect(config.mongodb.uri, mongoConnectSetting)
app.db = mongoose.connection
app.db.on('error', function (error) {
console.error('Error in MongoDb connection: ' + error)
mongoose.disconnect()
})
app.db.on('disconnected', function () {
console.log('MongoDB disconnected!')
mongoose.connect(config.mongodb.uri, mongoConnectSetting)
})
// config data models
require('./models')(app, mongoose)
// settings
app.disable('x-powered-by')
app.set('port', config.port)
app.set('views', path.join(__dirname, 'controllers'))
app.set('view engine', 'pug')
// middleware
app.use(require('morgan')('dev'))
app.use(require('compression')())
app.use(require('serve-static')(path.join(__dirname, 'build')))
app.use(require('serve-static')(path.join(__dirname, 'public')))
app.use(require('method-override')())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cookieParser(config.cryptoKey))
app.use(passport.initialize())
app.use(cors())
// app.use(csrf({ cookie: { signed: false } }))
helmet(app)
// response locals
app.use(function (req, res, next) {
// res.cookie('_csrfToken', req.csrfToken())
res.locals.user = {}
res.locals.user.defaultReturnUrl = req.user && req.user.defaultReturnUrl()
res.locals.user.username = req.user && req.user.username
next()
})
// global locals
app.locals.projectName = app.config.projectName
app.locals.copyrightYear = new Date().getFullYear()
app.locals.copyrightName = app.config.companyName
app.locals.cacheBreaker = 'br34k-01'
// setup passport
require('./passport')(app, passport)
// setup routes
require('./routes')(app, passport)
// setup react
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, './build/index.html'))
})
// custom (friendly) error handler
// app.use(require('./controllers/http/index').http500)
// setup utilities
app.utility = {}
app.utility.sendmail = require('./util/sendmail')
app.utility.slugify = require('./util/slugify')
app.utility.workflow = require('./util/workflow')
app.utility.ODMService = require('./util/odm-service')
// listen up
app.server.listen(app.config.port, function () {
// and... we're live
console.log('Server is running on port ' + config.port)
})