开发模式下使用 Vite

This commit is contained in:
2024-05-16 20:38:58 +08:00
parent 50ba876daf
commit beef70cb4d
3 changed files with 18 additions and 7 deletions

4
index.html Normal file
View File

@ -0,0 +1,4 @@
<!DOCTYPE html>
<meta charset="UTF-8">
<title>DEMO</title>
<script defer type="module" src="./index.js"></script>

View File

@ -7,6 +7,7 @@ import random from 'string-random'
import formidable from 'formidable'
import crypto from 'crypto'
import HUB from './fmhub.js'
import { createServer } from 'vite'
const md5 = (str) => crypto.createHash('md5').update(str).digest('hex')
const databases = new Map() // 所有数据库
@ -397,17 +398,22 @@ const db_compact = function (req, res) {
// 读取对象列表
function index_get(req, res) {
// 返回对象列表
let list = []
databases.forEach((value, key) => {
list.push(key)
})
databases.forEach((value, key) => list.push(key))
res.json(list)
}
const app = expressWs(express()).app
const ServerSentEventsClient = {}
// 开发模式下使用 Vite
if (process.argv.includes('--dev')) {
const vite = await createServer({ server: { middlewareMode: 'html' } })
app.use(vite.middlewares)
} else {
app.use(express.static('dist'))
}
// Server-Sent Events (发送版本号来决定是否接收更新?)
app.use((req, res, next) => {
if (req.get('Accept') === 'text/event-stream') {

View File

@ -19,10 +19,11 @@
"string-random": "^0.1.3"
},
"scripts": {
"dev": "nodemon --watch index.js index.js",
"dev": "nodemon --watch index.js index.js --dev",
"start": "node index.js"
},
"devDependencies": {
"concurrently": "^8.2.2",
"nodemon": "^3.1.0",
"vite": "^5.2.11"
}