Spaces:
Running
Running
Update server.js
Browse files
server.js
CHANGED
@@ -1,12 +1,11 @@
|
|
|
|
1 |
process.on('uncaughtException', (err) => {
|
2 |
console.error('[Fatal Error] Uncaught Exception:', err.stack || err.message || err);
|
3 |
-
//
|
4 |
-
process.exit(1); // 可选择性地退出进程
|
5 |
});
|
6 |
|
7 |
process.on('unhandledRejection', (reason, promise) => {
|
8 |
console.error('[Warning] Unhandled Rejection at:', promise, 'reason:', reason);
|
9 |
-
// 可以选择是否记录后继续运行
|
10 |
});
|
11 |
|
12 |
// === 引入依赖 ===
|
@@ -15,14 +14,21 @@ const bodyParser = require('body-parser');
|
|
15 |
const cors = require('cors');
|
16 |
const requestIp = require('request-ip');
|
17 |
const crypto = require('crypto'); // 用于生成设备指纹的哈希值
|
18 |
-
const { URL } = require('url');
|
19 |
|
20 |
const app = express();
|
21 |
|
22 |
// === 配置常量 ===
|
23 |
-
const PORT = process.env.PORT;
|
24 |
const HOST = '0.0.0.0';
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
// === 中间件配置 ===
|
28 |
// CORS 配置
|
@@ -61,30 +67,20 @@ setInterval(() => {
|
|
61 |
}
|
62 |
}, CLEANUP_INTERVAL);
|
63 |
|
64 |
-
//
|
65 |
app.get("/", (req, res) => {
|
66 |
-
res.status(200).json({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
});
|
68 |
|
69 |
-
// ===
|
70 |
-
// 生成设备指纹
|
71 |
-
const generateDeviceFingerprint = (req) => {
|
72 |
-
const ip = req.clientIp || '';
|
73 |
-
const userAgent = req.headers['user-agent'] || '';
|
74 |
-
const acceptLanguage = req.headers['accept-language'] || '';
|
75 |
-
const connection = req.headers['connection'] || '';
|
76 |
-
const encoding = req.headers['accept-encoding'] || '';
|
77 |
-
const forwardedFor = req.headers['x-forwarded-for'] || '';
|
78 |
-
|
79 |
-
// 将关键信息合并生成唯一指纹
|
80 |
-
const rawFingerprint = `${ip}-${userAgent}-${acceptLanguage}-${connection}-${encoding}-${forwardedFor}`;
|
81 |
-
|
82 |
-
// 使用 SHA-256 哈希算法生成指纹
|
83 |
-
const fingerprint = crypto.createHash('sha256').update(rawFingerprint).digest('hex');
|
84 |
-
return fingerprint;
|
85 |
-
};
|
86 |
-
|
87 |
-
// === 路由 ===
|
88 |
|
89 |
// 存储 URL(POST 请求)
|
90 |
app.post('/storeURL', (req, res) => {
|
@@ -138,11 +134,11 @@ app.get('/getURL', (req, res) => {
|
|
138 |
// === 启动服务器 ===
|
139 |
app.listen(PORT, HOST, (err) => {
|
140 |
if (err) {
|
141 |
-
console.error(
|
142 |
-
|
143 |
}
|
144 |
-
console.log(`Server
|
145 |
});
|
146 |
|
147 |
-
|
148 |
-
console.log("Environment Variables:", process.env);
|
|
|
1 |
+
// === 全局异常处理(放在顶部) ===
|
2 |
process.on('uncaughtException', (err) => {
|
3 |
console.error('[Fatal Error] Uncaught Exception:', err.stack || err.message || err);
|
4 |
+
process.exit(1); // 可选择退出服务进程
|
|
|
5 |
});
|
6 |
|
7 |
process.on('unhandledRejection', (reason, promise) => {
|
8 |
console.error('[Warning] Unhandled Rejection at:', promise, 'reason:', reason);
|
|
|
9 |
});
|
10 |
|
11 |
// === 引入依赖 ===
|
|
|
14 |
const cors = require('cors');
|
15 |
const requestIp = require('request-ip');
|
16 |
const crypto = require('crypto'); // 用于生成设备指纹的哈希值
|
17 |
+
const { URL } = require('url'); // 用于验证 URL 格式
|
18 |
|
19 |
const app = express();
|
20 |
|
21 |
// === 配置常量 ===
|
22 |
+
const PORT = process.env.PORT; // 动态分配的端口
|
23 |
const HOST = '0.0.0.0';
|
24 |
+
|
25 |
+
// 检查 PORT 是否有效
|
26 |
+
if (!PORT) {
|
27 |
+
console.error("[Error] PORT environment variable is undefined. This is required by Hugging Face Spaces.");
|
28 |
+
process.exit(1); // 如果 PORT 未定义,立即退出
|
29 |
+
}
|
30 |
+
|
31 |
+
console.log(`Starting server with HOST: ${HOST}, PORT: ${PORT}`);
|
32 |
|
33 |
// === 中间件配置 ===
|
34 |
// CORS 配置
|
|
|
67 |
}
|
68 |
}, CLEANUP_INTERVAL);
|
69 |
|
70 |
+
// 健康检查路由
|
71 |
app.get("/", (req, res) => {
|
72 |
+
res.status(200).json({
|
73 |
+
message: "Server is healthy and running!",
|
74 |
+
host: HOST,
|
75 |
+
port: PORT, // 当前监听的端口
|
76 |
+
env: {
|
77 |
+
PORT: process.env.PORT,
|
78 |
+
NODE_ENV: process.env.NODE_ENV,
|
79 |
+
},
|
80 |
+
});
|
81 |
});
|
82 |
|
83 |
+
// === 路由与业务逻辑 ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
// 存储 URL(POST 请求)
|
86 |
app.post('/storeURL', (req, res) => {
|
|
|
134 |
// === 启动服务器 ===
|
135 |
app.listen(PORT, HOST, (err) => {
|
136 |
if (err) {
|
137 |
+
console.error("[Error] Server failed to start:", err);
|
138 |
+
process.exit(1); // 如果存在错误,立即退出
|
139 |
}
|
140 |
+
console.log(`Server successfully started on http://${HOST}:${PORT}`);
|
141 |
});
|
142 |
|
143 |
+
// 打印环境变量(调试使用)
|
144 |
+
console.log("Environment Variables:", process.env);
|