diff options
| author | Pitu <[email protected]> | 2021-01-20 14:01:36 +0900 |
|---|---|---|
| committer | Pitu <[email protected]> | 2021-01-20 14:01:36 +0900 |
| commit | 91a15f417e73aea5b4fe002ae5abaf70c046b47f (patch) | |
| tree | afa54231d68bab0846a5e7f47bf5eb15ef41857f /src/api/structures/Server.js | |
| parent | chore: update nuxt (diff) | |
| download | host.fuwn.me-91a15f417e73aea5b4fe002ae5abaf70c046b47f.tar.xz host.fuwn.me-91a15f417e73aea5b4fe002ae5abaf70c046b47f.zip | |
feat: enable ssr and serve it with the api
Diffstat (limited to 'src/api/structures/Server.js')
| -rw-r--r-- | src/api/structures/Server.js | 25 |
1 files changed, 7 insertions, 18 deletions
diff --git a/src/api/structures/Server.js b/src/api/structures/Server.js index 446c621..268ba68 100644 --- a/src/api/structures/Server.js +++ b/src/api/structures/Server.js @@ -5,6 +5,7 @@ if (!process.env.SERVER_PORT) { process.exit(0); } +const { loadNuxt, build } = require('nuxt'); const express = require('express'); const helmet = require('helmet'); const cors = require('cors'); @@ -80,25 +81,13 @@ class Server { }); } - serveNuxt() { - // Serve the frontend if we are in production mode - if (process.env.NODE_ENV === 'production') { - this.server.use(express.static(path.join(__dirname, '../../../dist'))); + async serveNuxt() { + const isProd = process.env.NODE_ENV === 'production'; + const nuxt = await loadNuxt(isProd ? 'start' : 'dev'); + this.server.use(nuxt.render); + if (!isProd) { + build(nuxt); } - - /* - For vue router to work with express we need this fallback. - After all the routes are loaded and the static files handled and if the - user is trying to access a non-mapped route we serve the website instead - since it has routes of it's own that don't work if accessed directly - */ - this.server.all('*', (_req, res) => { - try { - res.sendFile(path.join(__dirname, '../../../dist/index.html')); - } catch (error) { - res.json({ success: false, message: 'Something went wrong' }); - } - }); } createJobs() { |