Spaces:
Running
Running
Update server.js
Browse files
server.js
CHANGED
@@ -1,3 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
// === 引入依赖 ===
|
2 |
const express = require('express');
|
3 |
const bodyParser = require('body-parser');
|
@@ -9,12 +20,11 @@ const { URL } = require('url'); // 用于验证 URL 格式
|
|
9 |
const app = express();
|
10 |
|
11 |
// === 配置常量 ===
|
12 |
-
const PORT = process.env.PORT
|
13 |
const HOST = '0.0.0.0';
|
14 |
console.log(`Starting server! PORT: ${PORT}`);
|
15 |
|
16 |
// === 中间件配置 ===
|
17 |
-
|
18 |
// CORS 配置
|
19 |
const corsOptions = {
|
20 |
origin: '*', // 允许所有来源的请求
|
@@ -53,7 +63,7 @@ setInterval(() => {
|
|
53 |
|
54 |
// 健康检查
|
55 |
app.get("/", (req, res) => {
|
56 |
-
res.status(200).
|
57 |
});
|
58 |
|
59 |
// === 辅助函数 ===
|
@@ -134,11 +144,5 @@ app.listen(PORT, HOST, (err) => {
|
|
134 |
console.log(`Server running on http://${HOST}:${PORT}`);
|
135 |
});
|
136 |
|
137 |
-
// === 全局错误处理 ===
|
138 |
-
process.on('uncaughtException', (err) => {
|
139 |
-
console.error('Uncaught Exception:', err);
|
140 |
-
});
|
141 |
|
142 |
-
|
143 |
-
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
144 |
-
});
|
|
|
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 |
// === 引入依赖 ===
|
13 |
const express = require('express');
|
14 |
const bodyParser = require('body-parser');
|
|
|
20 |
const app = express();
|
21 |
|
22 |
// === 配置常量 ===
|
23 |
+
const PORT = process.env.PORT;
|
24 |
const HOST = '0.0.0.0';
|
25 |
console.log(`Starting server! PORT: ${PORT}`);
|
26 |
|
27 |
// === 中间件配置 ===
|
|
|
28 |
// CORS 配置
|
29 |
const corsOptions = {
|
30 |
origin: '*', // 允许所有来源的请求
|
|
|
63 |
|
64 |
// 健康检查
|
65 |
app.get("/", (req, res) => {
|
66 |
+
res.status(200).json({ message: "Server is healthy and running!" });
|
67 |
});
|
68 |
|
69 |
// === 辅助函数 ===
|
|
|
144 |
console.log(`Server running on http://${HOST}:${PORT}`);
|
145 |
});
|
146 |
|
|
|
|
|
|
|
|
|
147 |
|
148 |
+
console.log("Environment Variables:", process.env);
|
|
|
|