Skip to content

Instantly share code, notes, and snippets.

@vherasme
Created November 1, 2019 09:41
Show Gist options
  • Save vherasme/4e1a0ebf79284db4e210ed569f7bd5c5 to your computer and use it in GitHub Desktop.
Save vherasme/4e1a0ebf79284db4e210ed569f7bd5c5 to your computer and use it in GitHub Desktop.
Generated Koa / Nuxt project
const Koa = require('koa')
const consola = require('consola')
const { Nuxt, Builder } = require('nuxt')
const router = require("./routes");
const app = new Koa()
// Import and Set Nuxt.js options
const config = require('../nuxt.config.js')
config.dev = app.env !== 'production'
async function start() {
// Instantiate nuxt.js
const nuxt = new Nuxt(config)
const {
host = process.env.HOST || '127.0.0.1',
port = process.env.PORT || 3000
} = nuxt.options.server
// Build in development
if (config.dev) {
const builder = new Builder(nuxt)
await builder.build()
} else {
await nuxt.ready()
}
app.use((ctx) => {
ctx.status = 200
ctx.respond = false // Bypass Koa's built-in response handling
ctx.req.ctx = ctx // This might be useful later on, e.g. in nuxtServerInit or with nuxt-stash
nuxt.render(ctx.req, ctx.res)
});
//Add routes to retrieve and save articles
app.use(router.routes());
app.use(router.allowedMethods());
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