Small refactor, added ViteJS debug logging and server.js for serving dynamic routes in dev mode

This commit is contained in:
2022-07-01 01:50:06 +02:00
parent 7b9204db4a
commit 2d1ecedcf0
5 changed files with 2299 additions and 1162 deletions
+2232 -1150
View File
File diff suppressed because it is too large Load Diff
+4 -3
View File
@@ -3,13 +3,14 @@
"version": "1.0.0",
"description": "Javascript frontend for this Golang server built using ViteJS",
"main": "index.js",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"start": "vite preview"
"dev": "node server.js",
"build": "vite build"
},
"license": "MIT",
"devDependencies": {
"express": "^4.18.1",
"sass": "^1.53.0",
"vite": "^2.9.13"
}
+34
View File
@@ -0,0 +1,34 @@
import { dirname, resolve } from 'path'
import express from 'express'
import { createServer as createViteServer } from 'vite'
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
async function createServer(customHtmlRoutes) {
const app = express()
// In middleware mode, if you want to use Vite's own HTML serving logic
// use `'html'` as the `middlewareMode` (ref https://vitejs.dev/config/#server-middlewaremode)
const vite = await createViteServer({
server: { middlewareMode: 'html' },
})
for (const [route, file] of Object.entries(customHtmlRoutes)) {
app.get(route, (req, res) => {
const filePath = resolve(__dirname, file)
console.log(`Custom Route: %s`, req.url)
return res.sendFile(filePath)
})
}
app.use(vite.middlewares)
app.listen(3000)
}
createServer({
// Useful for developing with dynamic routes
'/': './index.html',
})