export default async function(req: Request): Promise<Response> {
const url = new URL(req.url);
const pathname = url.pathname;
if (pathname === "/") {
return serveHomePage();
} else if (pathname === "/story1") {
return serveStoryPage("First Fake Story", "This is the content of the first fake story.");
} else if (pathname === "/story2") {
return serveStoryPage("Second Fake Story", "This is the content of the second fake story.");
} else if (pathname === "/story3") {
return serveStoryPage("Third Fake Story", "This is the content of the third fake story.");
} else {
return new Response("Page not found", { status: 404 });
}
}
function serveHomePage(): Response {
const htmlContent = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hacker News Clone</title>
<style>
body { font-family: Verdana, Geneva, sans-serif; margin: 0; padding: 0; background: #f6f6ef; }
#header { background: #ff6600; padding: 8px; }
#header a { color: black; font-size: 16px; text-decoration: none; font-weight: bold; }
.container { width: 85%; margin: auto; }
.story { padding: 4px 0; border-bottom: 1px solid #ccc; }
.story-title a { font-size: 14px; color: #000; text-decoration: none; }
.story-title a:hover { text-decoration: underline; }
.story-details { font-size: 10px; color: #828282; }
</style>
</head>
<body>
<div id="header" class="container">
<a href="/">Hacker News Clone</a>
</div>
<div class="container">
<div class="story">
<div class="story-title"><a href="/story1">First Fake Story</a></div>
<div class="story-details">100 points by user1 1 hour ago | 50 comments</div>
</div>
<div class="story">
<div class="story-title"><a href="/story2">Second Fake Story</a></div>
<div class="story-details">150 points by user2 2 hours ago | 75 comments</div>
</div>
<div class="story">
<div class="story-title"><a href="/story3">Third Fake Story</a></div>
<div class="story-details">200 points by user3 3 hours ago | 100 comments</div>
</div>
</div>
</body>
</html>
`;
return new Response(htmlContent, {
headers: {
"Content-Type": "text/html",
},
});
}
function serveStoryPage(title: string, content: string): Response {
const htmlContent = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${title}</title>
<style>
body { font-family: Verdana, Geneva, sans-serif; margin: 0; padding: 0; background: #f6f6ef; }
#header { background: #ff6600; padding: 8px; }
#header a { color: black; font-size: 16px; text-decoration: none; font-weight: bold; }
.container { width: 85%; margin: auto; }
.story-content { padding: 16px 0; }
</style>
</head>
<body>
<div id="header" class="container">
<a href="/">Hacker News Clone</a>
</div>
<div class="container">
<h2>${title}</h2>
<div class="story-content">${content}</div>
</div>
</body>
</html>
`;
return new Response(htmlContent, {
headers: {
"Content-Type": "text/html",