Created
October 21, 2019 06:55
-
-
Save dasmikko/bef743ad4400f8b1c1c8c6e494f081d6 to your computer and use it in GitHub Desktop.
Nuxt Express server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Place this file inside the "/server" folder in the root project | |
*/ | |
const express = require('express') | |
const consola = require('consola') | |
const { Nuxt, Builder } = require('nuxt') | |
const app = express() | |
// 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(nuxt.render) | |
// Listen the server | |
app.listen(port, host) | |
consola.ready({ | |
message: `Server listening on http://${host}:${port}`, | |
badge: true | |
}) | |
} | |
start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment