1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// This val creates a simple Hacker News clone with clickable story titles.
// Each story title links to a separate page that displays some fake story content.
import { Hono } from "npm:hono";
const app = new Hono();
const stories = [
{ id: 1, title: "First Fake Story", content: "Content of the first fake story.", details: "100 points by user1 1 hour ago | 50 comments" },
{ id: 2, title: "Second Fake Story", content: "Content of the second fake story.", details: "150 points by user2 2 hours ago | 75 comments" },
{ id: 3, title: "Third Fake Story", content: "Content of the third fake story.", details: "200 points by user3 3 hours ago | 100 comments" },
];
app.get("/", (c) => {
const storyList = stories.map(story => `
<div class="story">
<div class="story-title"><a href="/story/${story.id}">${story.title}</a></div>
<div class="story-details">${story.details}</div>
</div>
`).join("");
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">
${storyList}
</div>
</body>
</html>
`;
return c.html(htmlContent);
});
app.get("/story/:id", (c) => {
const storyId = c.req.param("id");
const story = stories.find(s => s.id === parseInt(storyId));
if (!story) {
return c.text("Story not found", 404);
}
const htmlContent = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${story.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: 20px 0; }
</style>
</head>
<body>
<div id="header" class="container">
<a href="/">Hacker News Clone</a>
</div>
<div class="container">
<h1>${story.title}</h1>
<div class="story-content">${story.content}</div>
</div>
</body>
</html>
`;
return c.html(htmlContent);
});
export default app.fetch;
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 17, 2024