-
Notifications
You must be signed in to change notification settings - Fork 25
/
app.js
43 lines (35 loc) · 1 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
const express = require('express')
const nuxt = require('nuxt')
const { Nuxt, Builder } = nuxt
const app = express()
const host = process.env.HOST || '127.0.0.1'
const port = process.env.PORT || 8080
// uncaughtException 避免程序崩溃
process.on('uncaughtException', function (err) {
console.error(err)
try {
let killTimer = setTimeout(function () {
process.exit(1)
}, 10000)
killTimer.unref()
server.close()
} catch (e) {
console.error('Fail to close server safely. ', e.stack)
}
})
app.set('port', port)
// Import and Set Nuxt.js options
const config = require('./nuxt.config.js')
config.dev = !(process.env.NODE_ENV === 'production')
// Init Nuxt.js
const server = new Nuxt(config)
// Build only in dev mode
if (config.dev) {
const builder = new Builder(server)
builder.build()
}
// Give nuxt middleware to express
app.use(server.render)
// Listen the server
app.listen(port, host)
console.log('Server listening on ' + host + ':' + port) // eslint-disable-line no-console