-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (45 loc) · 1.17 KB
/
index.js
File metadata and controls
56 lines (45 loc) · 1.17 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Community Server 主入口
* 随舞社区API服务器
*/
import cors from 'cors';
import express from 'express';
import { CONFIG } from './config/constants.js';
import { registerRoutes } from './routes/index.js';
import { errorHandler, notFoundHandler } from './middleware/error.middleware.js';
const app = express();
function createCorsOptions() {
if (CONFIG.CORS_ORIGIN === '*') {
return {
origin: true,
credentials: false
};
}
const allowedOrigins = CONFIG.CORS_ORIGIN
.split(',')
.map((item) => item.trim())
.filter(Boolean);
return {
origin(origin, callback) {
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
return;
}
callback(new Error('CORS origin is not allowed.'));
},
credentials: true
};
}
app.disable('x-powered-by');
// 中间件配置
app.use(cors(createCorsOptions()));
app.use(express.json({ limit: '1mb' }));
// 注册路由
registerRoutes(app);
// 错误处理
app.use(notFoundHandler);
app.use(errorHandler);
// 启动服务器
app.listen(CONFIG.PORT, () => {
console.log(`[community-server] listening on http://localhost:${CONFIG.PORT}`);
});