gancio/server/index.js

57 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-06-07 17:02:33 +02:00
#!/bin/env node
const path = require('path')
2019-04-03 00:25:12 +02:00
const express = require('express')
const consola = require('consola')
2019-04-23 15:45:52 +02:00
const morgan = require('morgan')
2019-06-06 23:54:32 +02:00
const { Nuxt, Builder } = require('nuxt')
2019-06-07 17:02:33 +02:00
const firstRun = require('./firstrun')
2019-04-03 00:25:12 +02:00
// Import and Set Nuxt.js options
const nuxt_config = require('../nuxt.config.js')
const config = require('./config')
2019-04-03 00:25:12 +02:00
2019-06-07 17:02:33 +02:00
const app = express()
2019-04-03 00:25:12 +02:00
async function start() {
// Init Nuxt.js
const nuxt = new Nuxt(nuxt_config)
2019-04-03 00:25:12 +02:00
// const { host, port } = nuxt.options.server
2019-04-03 00:25:12 +02:00
// Build only in dev mode
if (nuxt_config.dev) {
2019-04-03 00:25:12 +02:00
const builder = new Builder(nuxt)
await builder.build()
} else {
await nuxt.ready()
}
// Give nuxt middleware to express
2019-04-23 15:45:52 +02:00
app.use(morgan('dev'))
app.use('/media/', express.static(path.join(__dirname, '..', 'uploads')))
2019-04-03 00:25:12 +02:00
app.use(nuxt.render)
// Listen the server
const server = app.listen(config.server)
// close connections/port/unix socket
function shutdown() {
consola.info(`Closing connections..`)
server.close()
}
process.on('SIGTERM', shutdown)
process.on('SIGINT', shutdown)
server.on('error', e => {
consola.error(e)
})
server.on('listening', () => {
const address = server.address()
consola.ready({
message: `Server listening on ${(typeof address) === 'object' ? `${address.address}:${address.port}` : address}`,
badge: true
})
2019-04-03 00:25:12 +02:00
})
}
2019-06-06 23:54:32 +02:00
2019-06-07 17:02:33 +02:00
firstRun.then(start)