| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- const Koa = require('koa')
- const cors = require('koa2-cors');
- const consola = require('consola')
- const { Nuxt, Builder } = require('nuxt')
- const router = require('./routes/index');
- const app = new Koa()
- // routes
- app.use(router().routes(), router().allowedMethods());
- app.use(cors({
- origin: 'http://localhost:3000',
- // exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
- maxAge: 3600,
- credentials: true,
- allowMethods: ['GET', 'POST', 'OPTIONS'],
- allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
- }));
- // 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 || 8000
- } = nuxt.options.server
- await nuxt.ready()
- // Build in development
- if (config.dev) {
- const builder = new Builder(nuxt)
- await builder.build()
- }
- 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)
- })
- app.listen(port, host)
- consola.ready({
- message: `Server listening on http://${host}:${port}`,
- badge: true
- })
- }
- start()
|