Mon Oct 15 2018
Copied to clipboard! Copy reply
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

const Koa = require('koa')
const consola = require('consola')
const { Nuxt, Builder } = require('nuxt')

const app = new Koa()
const host = process.env.HOST || '127.0.0.1'
const port = process.env.PORT || 3000

// Import and Set Nuxt.js options
let config = require('../nuxt.config.js')
config.dev = (app.env === 'production')

console.log('ay!')
// Instantiate nuxt.js
const nuxt = new Nuxt(config)

app.use(async ctx => {
  ctx.status = 200 // koa defaults to 404 when it sees that status is unset
  
  await nuxt.render(ctx.req, ctx.res)
  /*
  return new Promise((resolve, reject) => {
    ctx.res.on('close', resolve)
    ctx.res.on('finish', resolve)
    console.log('2!')
    nuxt.render(ctx.req, ctx.res, promise => {
      
      promise.then(resolve).catch(reject)
    })
  })
  */
  
})

// Build in development
if (config.dev)
{
  new Builder(nuxt).build()
    .then(listen)
    .catch((error) => {
      console.error(error)
      process.exit(1)
    })
}
else
{ 
  

  listen()
}

  function listen() {
    // Listen the app
    app.listen(port, () => {
      console.log(`App listening on port ${port}`);
    });
  }