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