-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
45 lines (37 loc) · 1.11 KB
/
server.ts
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
41
42
43
44
45
import express from 'express'
import router from './routers'
import cors from 'cors'
import path from 'path'
import cookieParser from 'cookie-parser'
import {
HOST_IP,
REACT_APP_PORT,
FILE_SERVER_PORT,
CORS_HOST_IP,
} from './controllers/port'
const app = express()
// 使用cookieParser中间件
app.use(cookieParser())
app.use(express.json())
// 设置允许跨域请求的选项
const corsOptions = {
origin: CORS_HOST_IP(), // 允许的请求来源
credentials: true, // 允许携带认证信息(如cookies)
}
console.log('[INFO](server.ts) CORS:', CORS_HOST_IP())
console.log('[INFO](server.ts) HOST_IP:', HOST_IP())
console.log('[INFO](server.ts) REACT_APP_PORT:', REACT_APP_PORT())
console.log('[INFO](server.ts) FILE_SERVER_PORT:', FILE_SERVER_PORT())
// 解析请求体
app.use(express.json())
// 应用cors中间件
app.use(cors(corsOptions))
// 设置静态文件目录
app.use(express.static(path.join(process.cwd(), 'public')))
// 使用路由
app.use(router)
app.listen(FILE_SERVER_PORT(), () => {
console.log(
`[PASS] Server is running on http://${HOST_IP()}:${FILE_SERVER_PORT()}`
)
})