-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
61 lines (48 loc) · 1.37 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
/* eslint no-console: 0 */
const { Nuxt, Builder } = require('nuxt')
const app = require('express')()
const logger = require('morgan')
const config = require('config')
const session = require('express-session')
const MongoStore = require('connect-mongo')
process.env.PORT = config.port
const isDev = app.get('env') !== 'production'
app.use(logger(isDev ? 'dev' : 'combined'))
// To use X-Forwarded-Host as req.hostname
app.enable('trust proxy')
app.use(session({
resave: false,
saveUninitialized: false,
secret: config.cookie.secret,
cookie: { maxAge: config.cookie.maxAge },
unset: 'destroy',
store: MongoStore.create({
mongoUrl: `mongodb://${config.mongo.host}:${config.mongo.port}/${config.mongo.db}`
})
}))
// Import API Routes
app.use('/api', require('./api'))
// API error handler
app.use((err, req, res, next) => {
res.status(err.status || 500)
res.write(isDev ? err.stack : err.message)
res.end()
})
// Import and Set Nuxt.js options
const nuxtConfig = require('./nuxt.config.js')
// Init Nuxt.js
const nuxt = new Nuxt({
...nuxtConfig,
// workaround: tell nuxt that we are running in production and we don't need buildModules
_start: !nuxtConfig.dev
})
app.use(nuxt.render)
// Build only in dev mode
if (nuxtConfig.dev) {
new Builder(nuxt).build()
.catch((error) => {
console.error(error)
process.exit(1)
})
}
module.exports = app