开发模式下使用 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,10 +7,11 @@ import random from 'string-random'
import formidable from 'formidable' import formidable from 'formidable'
import crypto from 'crypto' import crypto from 'crypto'
import HUB from './fmhub.js' import HUB from './fmhub.js'
import { createServer } from 'vite'
const md5 = (str) => crypto.createHash('md5').update(str).digest('hex') const md5 = (str) => crypto.createHash('md5').update(str).digest('hex')
const databases = new Map() // 所有数据库 const databases = new Map() // 所有数据库
const FM = new HUB() // 频道消息分发器 const FM = new HUB() // 频道消息分发器
const messagelist = new Map() // 消息队列的nedb存储 const messagelist = new Map() // 消息队列的nedb存储
const db = (name) => (databases.get(name) || function () { const db = (name) => (databases.get(name) || function () {
@ -397,17 +398,22 @@ const db_compact = function (req, res) {
// 读取对象列表 // 读取对象列表
function index_get(req, res) { function index_get(req, res) {
// 返回对象列表
let list = [] let list = []
databases.forEach((value, key) => { databases.forEach((value, key) => list.push(key))
list.push(key)
})
res.json(list) res.json(list)
} }
const app = expressWs(express()).app const app = expressWs(express()).app
const ServerSentEventsClient = {} 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 (发送版本号来决定是否接收更新?) // Server-Sent Events (发送版本号来决定是否接收更新?)
app.use((req, res, next) => { app.use((req, res, next) => {
if (req.get('Accept') === 'text/event-stream') { if (req.get('Accept') === 'text/event-stream') {

View File

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