start with nuxt
This commit is contained in:
36
server/index.js
Normal file
36
server/index.js
Normal file
@@ -0,0 +1,36 @@
|
||||
const express = require('express')
|
||||
const consola = require('consola')
|
||||
const { Nuxt, Builder } = require('nuxt')
|
||||
const app = express()
|
||||
const cors = require('cors')
|
||||
|
||||
// Import and Set Nuxt.js options
|
||||
const config = require('../nuxt.config.js')
|
||||
config.dev = !(process.env.NODE_ENV === 'production')
|
||||
|
||||
async function start() {
|
||||
// Init Nuxt.js
|
||||
const nuxt = new Nuxt(config)
|
||||
|
||||
const { host, port } = nuxt.options.server
|
||||
|
||||
// Build only in dev mode
|
||||
if (config.dev) {
|
||||
const builder = new Builder(nuxt)
|
||||
await builder.build()
|
||||
} else {
|
||||
await nuxt.ready()
|
||||
}
|
||||
|
||||
// Give nuxt middleware to express
|
||||
app.use(cors())
|
||||
app.use(nuxt.render)
|
||||
|
||||
// Listen the server
|
||||
app.listen(port, host)
|
||||
consola.ready({
|
||||
message: `Server listening on http://${host}:${port}`,
|
||||
badge: true
|
||||
})
|
||||
}
|
||||
start()
|
||||
Reference in New Issue
Block a user