Files
urbanLifeline/urbanLifelineWeb/packages/shared/server.js
2025-12-11 14:21:36 +08:00

74 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 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(``)
})