开发模式下使用 Vite
This commit is contained in:
parent
50ba876daf
commit
beef70cb4d
4
index.html
Normal file
4
index.html
Normal file
@ -0,0 +1,4 @@
|
||||
<!DOCTYPE html>
|
||||
<meta charset="UTF-8">
|
||||
<title>DEMO</title>
|
||||
<script defer type="module" src="./index.js"></script>
|
18
index.js
18
index.js
@ -7,10 +7,11 @@ 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() // 所有数据库
|
||||
const FM = new HUB() // 频道消息分发器
|
||||
const databases = new Map() // 所有数据库
|
||||
const FM = new HUB() // 频道消息分发器
|
||||
const messagelist = new Map() // 消息队列的nedb存储
|
||||
|
||||
const db = (name) => (databases.get(name) || function () {
|
||||
@ -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') {
|
||||
|
@ -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"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user