前端服务共享
This commit is contained in:
73
urbanLifelineWeb/packages/shared/server.js
Normal file
73
urbanLifelineWeb/packages/shared/server.js
Normal file
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* Shared 模块静态文件服务器
|
||||
* 提供构建后的 ES Module 文件供其他应用使用
|
||||
*
|
||||
* 使用方式:
|
||||
* 1. npm run build:esm # 先构建
|
||||
* 2. node server.js # 启动服务器
|
||||
*/
|
||||
|
||||
import express from 'express'
|
||||
import cors from 'cors'
|
||||
import path from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url)
|
||||
const __dirname = path.dirname(__filename)
|
||||
|
||||
const app = express()
|
||||
const PORT = 5000
|
||||
|
||||
// 启用 CORS
|
||||
app.use(cors())
|
||||
|
||||
// 静态文件服务:/shared/* -> dist/esm/*
|
||||
app.use('/shared', express.static(path.join(__dirname, 'dist/esm'), {
|
||||
setHeaders: (res, filepath) => {
|
||||
// 设置正确的 MIME 类型
|
||||
if (filepath.endsWith('.js')) {
|
||||
res.setHeader('Content-Type', 'application/javascript; charset=utf-8')
|
||||
}
|
||||
// 允许跨域
|
||||
res.setHeader('Access-Control-Allow-Origin', '*')
|
||||
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS')
|
||||
}
|
||||
}))
|
||||
|
||||
// 健康检查
|
||||
app.get('/health', (req, res) => {
|
||||
res.json({
|
||||
status: 'ok',
|
||||
port: PORT,
|
||||
modules: ['components', 'utils', 'api', 'composables', 'types']
|
||||
})
|
||||
})
|
||||
|
||||
// 模块列表
|
||||
app.get('/modules', (req, res) => {
|
||||
res.json({
|
||||
modules: {
|
||||
components: '/shared/components.js',
|
||||
utils: '/shared/utils.js',
|
||||
api: '/shared/api.js',
|
||||
composables: '/shared/composables.js',
|
||||
types: '/shared/types.js'
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
// 启动服务器
|
||||
app.listen(PORT, () => {
|
||||
console.log(`\n🚀 Shared 模块服务器已启动!`)
|
||||
console.log(``)
|
||||
console.log(`📦 提供以下模块:`)
|
||||
console.log(` - http://localhost:${PORT}/shared/components.js`)
|
||||
console.log(` - http://localhost:${PORT}/shared/utils.js`)
|
||||
console.log(` - http://localhost:${PORT}/shared/api.js`)
|
||||
console.log(` - http://localhost:${PORT}/shared/composables.js`)
|
||||
console.log(` - http://localhost:${PORT}/shared/types.js`)
|
||||
console.log(``)
|
||||
console.log(`🔍 健康检查:http://localhost:${PORT}/health`)
|
||||
console.log(`📋 模块列表:http://localhost:${PORT}/modules`)
|
||||
console.log(``)
|
||||
})
|
||||
Reference in New Issue
Block a user