You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.3 KiB
JavaScript

3 years ago
import { dirname, resolve } from 'path'
import express from 'express'
import { createServer as createViteServer } from 'vite'
import { fileURLToPath } from 'url'
import { readFileSync } from 'fs'
3 years ago
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, async (req, res) => {
3 years ago
const filePath = resolve(__dirname, file)
console.log(`Custom Route: %s`, req.url)
let htmlFile = readFileSync(filePath, 'utf8')
htmlFile = await vite.transformIndexHtml(req.originalUrl, htmlFile)
res.setHeader('Content-Type', 'text/html')
return res.send(htmlFile)
3 years ago
})
}
app.use(vite.middlewares)
app.listen(3000)
}
createServer({
'/': './index.html',
'/api-keys': './index.html',
'/admin': './index.html',
'/buckets': './index.html',
'/buckets/:bucket': './index.html',
3 years ago
})