index.js
1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import express from 'express'
import cors from 'cors'
import path from 'path'
import { fileURLToPath } from 'url'
import connectionRouter from './routes/connection.js'
import queryRouter from './routes/query.js'
import exportRouter from './routes/export.js'
import sqlManageRouter from './routes/sql-manage.js'
import schedulerRouter from './routes/scheduler.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const app = express()
const PORT = 4000
const HOSTNAME = "localhost"
// 中间件
app.use(cors())
app.use(express.json({ limit: '50mb' }))
// API路由
app.use('/api/connection', connectionRouter)
app.use('/api/query', queryRouter)
app.use('/api/export', exportRouter)
app.use('/api/sql-manage', sqlManageRouter)
app.use('/api/scheduler', schedulerRouter)
// 生产模式下服务前端静态文件
const distPath = path.join(__dirname, '..', 'dist')
app.use(express.static(distPath))
app.get('*', (req, res) => {
if (!req.path.startsWith('/api')) {
res.sendFile(path.join(distPath, 'index.html'))
}
})
app.listen(PORT, HOSTNAME,() => {
console.log(`\n SQL报表工具 - 后端服务已启动`)
console.log(` API地址: http://${HOSTNAME}:${PORT}`)
console.log(` 前端地址: http://${HOSTNAME}:3000\n`)
})