Spaces:
Sleeping
Sleeping
- .idea/git_toolbox_prj.xml +15 -0
- .idea/misc.xml +0 -8
- app.js +60 -40
.idea/git_toolbox_prj.xml
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="GitToolBoxProjectSettings">
|
4 |
+
<option name="commitMessageIssueKeyValidationOverride">
|
5 |
+
<BoolValueOverride>
|
6 |
+
<option name="enabled" value="true" />
|
7 |
+
</BoolValueOverride>
|
8 |
+
</option>
|
9 |
+
<option name="commitMessageValidationEnabledOverride">
|
10 |
+
<BoolValueOverride>
|
11 |
+
<option name="enabled" value="true" />
|
12 |
+
</BoolValueOverride>
|
13 |
+
</option>
|
14 |
+
</component>
|
15 |
+
</project>
|
.idea/misc.xml
CHANGED
@@ -3,9 +3,6 @@
|
|
3 |
<component name="AhkProjectSettings">
|
4 |
<option name="defaultAhkSdk" value="AutoHotkey v2.0.10" />
|
5 |
</component>
|
6 |
-
<component name="FormatOnSaveOptions">
|
7 |
-
<option name="myRunOnSave" value="false" />
|
8 |
-
</component>
|
9 |
<component name="GOROOT" url="file://$PROJECT_DIR$/../../../../Program Files/Go" />
|
10 |
<component name="GoLibraries">
|
11 |
<option name="indexEntireGoPath" value="true" />
|
@@ -243,9 +240,4 @@
|
|
243 |
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
244 |
<output url="file://$PROJECT_DIR$/out" />
|
245 |
</component>
|
246 |
-
<component name="VcsManagerConfiguration">
|
247 |
-
<option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" />
|
248 |
-
<option name="CHECK_NEW_TODO" value="false" />
|
249 |
-
<option name="NON_MODAL_COMMIT_POSTPONE_SLOW_CHECKS" value="false" />
|
250 |
-
</component>
|
251 |
</project>
|
|
|
3 |
<component name="AhkProjectSettings">
|
4 |
<option name="defaultAhkSdk" value="AutoHotkey v2.0.10" />
|
5 |
</component>
|
|
|
|
|
|
|
6 |
<component name="GOROOT" url="file://$PROJECT_DIR$/../../../../Program Files/Go" />
|
7 |
<component name="GoLibraries">
|
8 |
<option name="indexEntireGoPath" value="true" />
|
|
|
240 |
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
241 |
<output url="file://$PROJECT_DIR$/out" />
|
242 |
</component>
|
|
|
|
|
|
|
|
|
|
|
243 |
</project>
|
app.js
CHANGED
@@ -2,57 +2,84 @@ import express from 'express';
|
|
2 |
import cors from 'cors';
|
3 |
|
4 |
const app = express();
|
5 |
-
// Hugging Face Spaces 默认使用 7860 端口
|
6 |
const PORT = process.env.PORT || 7860;
|
7 |
|
8 |
-
// 配置 CORS
|
9 |
app.use(cors());
|
10 |
|
11 |
-
//
|
12 |
app.use((req, res, next) => {
|
13 |
-
|
14 |
-
|
15 |
});
|
16 |
|
17 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
app.get('/', (req, res) => {
|
19 |
-
|
20 |
<!DOCTYPE html>
|
21 |
<html lang="en">
|
22 |
<head>
|
23 |
<meta charset="UTF-8">
|
24 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
25 |
-
<title>Express App
|
26 |
<style>
|
27 |
-
body { font-family: sans-serif; line-height: 1.6; padding: 2em; background-color: #
|
28 |
-
.container { max-width: 800px; margin: auto; background: white; padding: 2em; border-radius:
|
29 |
-
h1 { color: #
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
33 |
</style>
|
34 |
</head>
|
35 |
<body>
|
36 |
<div class="container">
|
37 |
-
<h1>Express.js
|
38 |
-
<p
|
39 |
-
<
|
40 |
-
|
41 |
-
console.log('Status:', response.status);
|
42 |
-
console.log('Custom Header "X-Custom-Header-Test":', response.headers.get('X-Custom-Header-Test'));
|
43 |
-
return response.json();
|
44 |
-
})
|
45 |
-
.then(data => console.log('Data:', data))
|
46 |
-
.catch(error => console.error('Error:', error));</code>
|
47 |
-
<p class="copy-notice">点击上面的代码块即可复制。</p>
|
48 |
</div>
|
|
|
49 |
<script>
|
50 |
-
document.getElementById('
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
});
|
57 |
</script>
|
58 |
</body>
|
@@ -60,14 +87,7 @@ app.get('/', (req, res) => {
|
|
60 |
`);
|
61 |
});
|
62 |
|
63 |
-
//
|
64 |
-
app.get('/api', (req, res) => {
|
65 |
-
res.json({
|
66 |
-
message: 'API is working!',
|
67 |
-
timestamp: new Date().toISOString()
|
68 |
-
});
|
69 |
-
});
|
70 |
-
|
71 |
app.listen(PORT, () => {
|
72 |
-
|
73 |
});
|
|
|
2 |
import cors from 'cors';
|
3 |
|
4 |
const app = express();
|
|
|
5 |
const PORT = process.env.PORT || 7860;
|
6 |
|
7 |
+
// 配置 CORS 中间件
|
8 |
app.use(cors());
|
9 |
|
10 |
+
// 中间件:为所有响应添加自定义头
|
11 |
app.use((req, res, next) => {
|
12 |
+
res.setHeader('X-Custom-Header-Test', 'HuggingFace-Express-App-Works');
|
13 |
+
next();
|
14 |
});
|
15 |
|
16 |
+
// API 路由,返回 JSON 数据
|
17 |
+
app.get('/api', (req, res) => {
|
18 |
+
res.json({
|
19 |
+
message: 'API is working!',
|
20 |
+
timestamp: new Date().toISOString(),
|
21 |
+
deployedOn: 'Hugging Face Spaces'
|
22 |
+
});
|
23 |
+
});
|
24 |
+
|
25 |
+
// 根路由,返回一个交互式的 HTML 测试页面
|
26 |
app.get('/', (req, res) => {
|
27 |
+
res.send(`
|
28 |
<!DOCTYPE html>
|
29 |
<html lang="en">
|
30 |
<head>
|
31 |
<meta charset="UTF-8">
|
32 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
33 |
+
<title>Express App Tester</title>
|
34 |
<style>
|
35 |
+
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; padding: 2em; background-color: #f9fafb; color: #111827; }
|
36 |
+
.container { max-width: 800px; margin: auto; background: white; padding: 2em; border-radius: 12px; box-shadow: 0 4px 20px rgba(0,0,0,0.05); }
|
37 |
+
h1 { color: #1f2937; }
|
38 |
+
button { font-size: 1em; font-weight: bold; color: white; background-color: #3b82f6; border: none; padding: 0.8em 1.5em; border-radius: 8px; cursor: pointer; transition: background-color 0.2s; }
|
39 |
+
button:hover { background-color: #2563eb; }
|
40 |
+
#results { background-color: #f3f4f6; padding: 1em; border-radius: 8px; margin-top: 1.5em; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New", Courier, monospace; }
|
41 |
+
.loading { color: #6b7280; }
|
42 |
+
.error { color: #ef4444; }
|
43 |
</style>
|
44 |
</head>
|
45 |
<body>
|
46 |
<div class="container">
|
47 |
+
<h1>Express.js 应用测试页面</h1>
|
48 |
+
<p>点击下面的按钮,页面将自动向 <code>/api</code> 端点发起一个 fetch 请求,并在此处显示完整的响应结果。</p>
|
49 |
+
<button id="test-btn">测试 API</button>
|
50 |
+
<pre id="results">点击按钮开始测试...</pre>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
</div>
|
52 |
+
|
53 |
<script>
|
54 |
+
const testButton = document.getElementById('test-btn');
|
55 |
+
const resultsContainer = document.getElementById('results');
|
56 |
+
|
57 |
+
testButton.addEventListener('click', async () => {
|
58 |
+
resultsContainer.textContent = '正在请求...';
|
59 |
+
resultsContainer.className = 'loading';
|
60 |
+
|
61 |
+
const apiUrl = window.location.origin + '/api';
|
62 |
+
|
63 |
+
try {
|
64 |
+
const response = await fetch(apiUrl);
|
65 |
+
const data = await response.json();
|
66 |
+
const customHeader = response.headers.get('X-Custom-Header-Test');
|
67 |
+
|
68 |
+
const resultText =
|
69 |
+
'--- RESPONSE ---\\n' +
|
70 |
+
'Status Code: ' + response.status + '\\n' +
|
71 |
+
'Status Text: ' + response.statusText + '\\n\\n' +
|
72 |
+
'Custom Header (X-Custom-Header-Test): ' + (customHeader || '未找到') + '\\n\\n' +
|
73 |
+
'Body (JSON):\\n' +
|
74 |
+
JSON.stringify(data, null, 2);
|
75 |
+
|
76 |
+
resultsContainer.textContent = resultText;
|
77 |
+
resultsContainer.className = '';
|
78 |
+
|
79 |
+
} catch (error) {
|
80 |
+
resultsContainer.textContent = '请求失败: \\n' + error;
|
81 |
+
resultsContainer.className = 'error';
|
82 |
+
}
|
83 |
});
|
84 |
</script>
|
85 |
</body>
|
|
|
87 |
`);
|
88 |
});
|
89 |
|
90 |
+
// 最终修正:此处的 console.log 语法是正确的。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
app.listen(PORT, () => {
|
92 |
+
console.log(`Server is running on port ${PORT}`);
|
93 |
});
|