fix: 优化docker 镜像
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
const http = require('http')
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
const express = require('express')
|
||||
const cors = require('cors')
|
||||
const { WebSocketServer } = require('ws')
|
||||
@@ -8,6 +10,7 @@ const { getSituation } = require('./situationData')
|
||||
const app = express()
|
||||
const PORT = process.env.API_PORT || 3001
|
||||
|
||||
app.set('trust proxy', 1)
|
||||
app.use(cors())
|
||||
app.use(express.json())
|
||||
app.use('/api', routes)
|
||||
@@ -17,6 +20,17 @@ app.post('/api/crawler/notify', (_, res) => {
|
||||
res.json({ ok: true })
|
||||
})
|
||||
|
||||
// 生产环境:提供前端静态文件
|
||||
const distPath = path.join(__dirname, '..', 'dist')
|
||||
if (fs.existsSync(distPath)) {
|
||||
app.use(express.static(distPath))
|
||||
app.get('*', (req, res, next) => {
|
||||
if (!req.path.startsWith('/api') && req.path !== '/ws') {
|
||||
res.sendFile(path.join(distPath, 'index.html'))
|
||||
} else next()
|
||||
})
|
||||
}
|
||||
|
||||
const server = http.createServer(app)
|
||||
|
||||
const wss = new WebSocketServer({ server, path: '/ws' })
|
||||
|
||||
Reference in New Issue
Block a user