From beef70cb4d4efeb638b245df81c92df096d55059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Thu, 16 May 2024 20:38:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=8F=91=E6=A8=A1=E5=BC=8F=E4=B8=8B?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20Vite?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 4 ++++ index.js | 18 ++++++++++++------ package.json | 3 ++- 3 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 index.html diff --git a/index.html b/index.html new file mode 100644 index 0000000..54ef1b7 --- /dev/null +++ b/index.html @@ -0,0 +1,4 @@ + + +DEMO + \ No newline at end of file diff --git a/index.js b/index.js index cc37b41..f904ee8 100644 --- a/index.js +++ b/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') { diff --git a/package.json b/package.json index 02c5a56..e30c1c8 100644 --- a/package.json +++ b/package.json @@ -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" }