1、新建文件夹 node-test 将打包dist 文件同步自定义本地服务文件夹node-test 中,安装依赖包。
npm install express serve-static cors
2、新创建服务文件js server.js 构建链接及端口
const express = require('express');
const path = require('path');
const cors = require('cors');
const app = express();
const PORT = 3000; // 自定义
// ✅ 允许跨域(如果需要)
app.use(cors());
// ✅ 静态资源目录,托管 dist/
app.use(express.static(path.join(__dirname, 'dist')));
// ✅ 解析 manifest.json,返回正确的 CDN 资源路径
app.get('/manifest', (req, res) => {
const manifest = require('./dist/manifest.json');
res.json(manifest);
});
// ✅ 兜底路由,返回 index.html(防止刷新 404)
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
// ✅ 启动服务器
app.listen(PORT, () => {
console.log(`🚀 Server is running at http://localhost:${PORT}`);
});
3、终端启动服务:
node server.js
浏览器查看已打包好的项目