index.js 963 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const Koa = require('koa')
  2. const consola = require('consola')
  3. const { Nuxt, Builder } = require('nuxt')
  4. const app = new Koa()
  5. // Import and Set Nuxt.js options
  6. const config = require('../nuxt.config.js')
  7. config.dev = app.env !== 'production'
  8. async function start () {
  9. // Instantiate nuxt.js
  10. const nuxt = new Nuxt(config)
  11. const {
  12. host = process.env.HOST || '127.0.0.1',
  13. port = process.env.PORT || 3000
  14. } = nuxt.options.server
  15. await nuxt.ready()
  16. // Build in development
  17. if (config.dev) {
  18. const builder = new Builder(nuxt)
  19. await builder.build()
  20. }
  21. app.use((ctx) => {
  22. ctx.status = 200
  23. ctx.respond = false // Bypass Koa's built-in response handling
  24. ctx.req.ctx = ctx // This might be useful later on, e.g. in nuxtServerInit or with nuxt-stash
  25. nuxt.render(ctx.req, ctx.res)
  26. })
  27. app.listen(port, host)
  28. consola.ready({
  29. message: `Server listening on http://${host}:${port}`,
  30. badge: true
  31. })
  32. }
  33. start()