Skip to content

Commit 3b933ca

Browse files
committed
update url
1 parent 115914a commit 3b933ca

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"devDependencies": {
3+
"wrangler": "^3.73.0"
4+
}
5+
}

public/script.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
document.addEventListener('DOMContentLoaded', async () => {
2-
const response = await fetch('/api/blog-posts')
3-
const blogPosts = await response.json()
4-
const timeline = document.querySelector('.timeline')
2+
const response = await fetch('https://path-momo-api.gusibi.workers.dev/momos');
3+
const blogPosts = await response.json();
4+
const timeline = document.querySelector('.timeline');
55

66
blogPosts.forEach(post => {
7-
const card = document.createElement('div')
8-
card.className = 'card'
7+
const card = document.createElement('div');
8+
card.className = 'card';
99
card.innerHTML = `
1010
<div class="header">
1111
<img src="avatar.png" alt="User Avatar">
@@ -16,7 +16,7 @@ document.addEventListener('DOMContentLoaded', async () => {
1616
<p>${post.body}</p>
1717
</div>
1818
<div class="time">${new Date(post.created_at).toLocaleTimeString()}</div>
19-
`
20-
timeline.appendChild(card)
21-
})
22-
})
19+
`;
20+
timeline.appendChild(card);
21+
});
22+
});

worker/index.js

+39-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,42 @@
1-
addEventListener('fetch', event => {
2-
event.respondWith(handleRequest(event.request))
3-
})
1+
export default {
2+
async fetch(request, env, ctx) {
3+
return await handleRequest(request);
4+
}
5+
};
46

57
async function handleRequest(request) {
6-
const response = await fetch('https://api.github.com/repos/{owner}/{repo}/issues')
7-
const issues = await response.json()
8-
const blogPosts = issues.map(issue => ({
9-
title: issue.title,
10-
body: issue.body,
11-
created_at: issue.created_at
12-
}))
13-
return new Response(JSON.stringify(blogPosts), {
14-
headers: { 'content-type': 'application/json' }
15-
})
8+
const GITHUB_TOKEN = ''; // 替换为你的 GitHub 个人访问令牌
9+
10+
try {
11+
const response = await fetch('https://api.github.com/repos/gusibi/path-meme-db/issues', {
12+
headers: {
13+
'Authorization': `token ${GITHUB_TOKEN}`,
14+
'Accept': 'application/vnd.github.v3+json'
15+
}
16+
});
17+
18+
// 检查响应是否成功
19+
if (!response.ok) {
20+
return new Response(`GitHub API request failed with status ${response.status}`, {
21+
status: response.status,
22+
headers: { 'content-type': 'text/plain' }
23+
});
24+
}
25+
26+
const issues = await response.json();
27+
const blogPosts = issues.map(issue => ({
28+
title: issue.title,
29+
body: issue.body,
30+
created_at: issue.created_at
31+
}));
32+
33+
return new Response(JSON.stringify(blogPosts), {
34+
headers: { 'content-type': 'application/json' }
35+
});
36+
} catch (error) {
37+
return new Response(`Error: ${error.message}`, {
38+
status: 500,
39+
headers: { 'content-type': 'text/plain' }
40+
});
41+
}
1642
}

0 commit comments

Comments
 (0)