-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
70 lines (64 loc) · 2.48 KB
/
Copy pathapp.js
File metadata and controls
70 lines (64 loc) · 2.48 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* @name app
* @author Mr·Fan DkPlusAI
* @Time 2026/03/24
* @description 项目服务启动入口,负责初始化 Express、中间件、跨域与路由。
**/
const path = require('path');
const express = require("express");
const os = require("os"); // 引入系统模块
const cors = require('cors');
const bodyParser = require('body-parser');
const routes = require('./routes');
const orginList = require('./config/Crossdomain');
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
const port = 3041;
// ====================== 中间件 ======================
app.use(bodyParser.json({ limit: '30mb' }));
app.use(bodyParser.urlencoded({ limit: '30mb', extended: true }));
app.use(express.json({ limit: '30mb' }));
app.use(express.urlencoded({ limit: '30mb', extended: true }));
app.use(cors());
// ====================== 跨域处理 ======================
app.all("*", function (req, res, next) {
if (orginList.includes(req.headers.origin)) {
res.header("Access-Control-Allow-Origin", req.headers.origin);
} else {
res.header("Access-Control-Allow-Origin", process.env.CORS_FALLBACK_ORIGIN || '*');
}
res.header("Access-Control-Allow-Headers", "content-type, Authorization");
res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
if (req.method.toLowerCase() === 'options') {
res.sendStatus(200);
} else {
next();
}
});
// ====================== 路由入口 ======================
app.use('/', routes);
// ====================== 获取本机IP函数 ======================
function getLocalIP() {
const interfaces = os.networkInterfaces();
for (const devName in interfaces) {
const iface = interfaces[devName];
for (let i = 0; i < iface.length; i++) {
const alias = iface[i];
if (alias.family === 'IPv4' && !alias.internal) {
return alias.address;
}
}
}
return '127.0.0.1';
}
const RUN_MODE = process.env.RUN_MODE || 'dev';
const NODE_ENV = process.env.NODE_ENV || 'development';
// ====================== 启动服务 ======================
const server = app.listen(port, '0.0.0.0', function () {
const localIP = getLocalIP();
console.log(`✅ Server started successfully!`);
console.log(`🚀 当前运行模式: ${RUN_MODE}`);
console.log(`🧱 NODE_ENV: ${NODE_ENV}`);
console.log(`📍 Local: http://127.0.0.1:${port}/`);
console.log(`🌐 Network: http://${localIP}:${port}/`);
});