Spaces:
Sleeping
Sleeping
- .idea/misc.xml +0 -3
- app.js +45 -48
.idea/misc.xml
CHANGED
@@ -7,9 +7,6 @@
|
|
7 |
<component name="GoLibraries">
|
8 |
<option name="indexEntireGoPath" value="true" />
|
9 |
</component>
|
10 |
-
<component name="GoVcsConfiguration">
|
11 |
-
<option name="GO_FMT" value="false" />
|
12 |
-
</component>
|
13 |
<component name="ProjectInspectionProfilesVisibleTreeState">
|
14 |
<entry key="Project Default">
|
15 |
<profile-state>
|
|
|
7 |
<component name="GoLibraries">
|
8 |
<option name="indexEntireGoPath" value="true" />
|
9 |
</component>
|
|
|
|
|
|
|
10 |
<component name="ProjectInspectionProfilesVisibleTreeState">
|
11 |
<entry key="Project Default">
|
12 |
<profile-state>
|
app.js
CHANGED
@@ -4,82 +4,80 @@ import cors from 'cors';
|
|
4 |
const app = express();
|
5 |
const PORT = process.env.PORT || 7860;
|
6 |
|
7 |
-
//
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
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 |
-
|
39 |
-
|
40 |
-
|
41 |
-
.
|
42 |
-
.error { color: #ef4444; }
|
43 |
</style>
|
44 |
</head>
|
45 |
<body>
|
46 |
<div class="container">
|
47 |
-
<h1>Express.js
|
48 |
-
<p
|
49 |
-
<
|
50 |
-
<
|
51 |
</div>
|
52 |
-
|
53 |
<script>
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
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,7 +85,6 @@ app.get('/', (req, res) => {
|
|
87 |
`);
|
88 |
});
|
89 |
|
90 |
-
// 最终修正:此处的 console.log 语法是正确的。
|
91 |
app.listen(PORT, () => {
|
92 |
console.log(`Server is running on port ${PORT}`);
|
93 |
});
|
|
|
4 |
const app = express();
|
5 |
const PORT = process.env.PORT || 7860;
|
6 |
|
7 |
+
// --- CORS 详细配置 (最终正确版本) ---
|
8 |
+
|
9 |
+
const corsOptions = {
|
10 |
+
origin: '*',
|
11 |
+
methods: ['GET', 'POST', 'PUT', 'DELETE'],
|
12 |
+
allowedHeaders: ['Content-Type', 'Authorization'],
|
13 |
+
// 只需暴露非 CORS 安全列表中的自定义头
|
14 |
+
exposedHeaders: ['X-Custom-Header-Test'],
|
15 |
+
credentials: false,
|
16 |
+
maxAge: 86400,
|
17 |
+
};
|
18 |
+
|
19 |
+
app.use(cors(corsOptions));
|
20 |
+
|
21 |
+
// --- 中间件和路由 ---
|
22 |
|
|
|
23 |
app.use((req, res, next) => {
|
24 |
res.setHeader('X-Custom-Header-Test', 'HuggingFace-Express-App-Works');
|
25 |
next();
|
26 |
});
|
27 |
|
|
|
28 |
app.get('/api', (req, res) => {
|
29 |
res.json({
|
30 |
+
message: 'API is working! CORS is correctly configured.',
|
31 |
timestamp: new Date().toISOString(),
|
|
|
32 |
});
|
33 |
});
|
34 |
|
|
|
35 |
app.get('/', (req, res) => {
|
36 |
+
const origin = `${req.protocol}://${req.get('host')}`;
|
37 |
+
// 在 fetch 代码中可以同时获取 Content-Length 和 X-Custom-Header-Test
|
38 |
+
const fetchCode = `fetch('${origin}/api')
|
39 |
+
.then(response => {
|
40 |
+
console.log('Status:', response.status);
|
41 |
+
// 'Content-Length' 是安全头,默认可访问
|
42 |
+
console.log('Content-Length:', response.headers.get('Content-Length'));
|
43 |
+
// 'X-Custom-Header-Test' 是非安全头,因已暴露,所以也可访问
|
44 |
+
console.log('Custom Header "X-Custom-Header-Test":', response.headers.get('X-Custom-Header-Test'));
|
45 |
+
return response.json();
|
46 |
+
})
|
47 |
+
.then(data => console.log('Data:', data))
|
48 |
+
.catch(error => console.error('Error:', error));`;
|
49 |
+
|
50 |
res.send(`
|
51 |
<!DOCTYPE html>
|
52 |
<html lang="en">
|
53 |
<head>
|
54 |
<meta charset="UTF-8">
|
55 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
56 |
+
<title>Express App on Hugging Face</title>
|
57 |
<style>
|
58 |
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; padding: 2em; background-color: #f9fafb; color: #111827; }
|
59 |
.container { max-width: 800px; margin: auto; background: white; padding: 2em; border-radius: 12px; box-shadow: 0 4px 20px rgba(0,0,0,0.05); }
|
60 |
h1 { color: #1f2937; }
|
61 |
+
p { font-size: 1.1em; }
|
62 |
+
pre { background-color: #f3f4f6; padding: 1.5em; border-radius: 8px; white-space: pre-wrap; word-wrap: break-word; }
|
63 |
+
code { font-family: "Courier New", Courier, monospace; font-size: 1.1em; }
|
64 |
+
.copy-notice { font-size: 0.9em; color: #6b7280; margin-top: 1em; text-align: center; }
|
|
|
65 |
</style>
|
66 |
</head>
|
67 |
<body>
|
68 |
<div class="container">
|
69 |
+
<h1>Express.js 应用已部署</h1>
|
70 |
+
<p>CORS 策略已正确配置。请复制以下代码到浏览器开发者工具中进行测试,它将同时读取一个安全头 (Content-Length) 和一个已暴露的自定义头 (X-Custom-Header-Test)。</p>
|
71 |
+
<pre><code id="fetch-code">${fetchCode}</code></pre>
|
72 |
+
<p class="copy-notice">点击上面的代码块即可复制。</p>
|
73 |
</div>
|
|
|
74 |
<script>
|
75 |
+
document.getElementById('fetch-code').parentElement.addEventListener('click', function() {
|
76 |
+
navigator.clipboard.writeText(document.getElementById('fetch-code').innerText).then(() => {
|
77 |
+
alert('代码已复制到剪贴板!');
|
78 |
+
}).catch(err => {
|
79 |
+
console.error('无法复制: ', err);
|
80 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
});
|
82 |
</script>
|
83 |
</body>
|
|
|
85 |
`);
|
86 |
});
|
87 |
|
|
|
88 |
app.listen(PORT, () => {
|
89 |
console.log(`Server is running on port ${PORT}`);
|
90 |
});
|