Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
export const quoteResponder = (request) => {
// eventually replace with https://docs.val.town/sqlite
const quotes = [
{
date: "2023-10-19",
quote:
"Happiness does not come from doing easy work but from the afterglow of satisfaction that comes after the achievement of a difficult task that demanded our best.",
author: "Theodore Isaac Rubin",
},
{
date: "2023-10-20",
quote:
"Just because something doesn't do what you planned it to do doesn't mean it's useless.",
author: "Thomas A. Edison",
},
{
date: "2023-10-21",
quote:
"Success is not final; failure is not fatal: it is the courage to continue that counts.",
author: "Winston Churchill",
},
{
date: "2023-10-22",
quote: "Life is what happens when you’re busy making other plans.",
author: "John Lennon",
},
{
date: "2023-10-23",
quote:
"Excellence is an art won by training and habituation. We do not act rightly because we have virtue or excellence, but we rather have those because we have acted rightly. We are what we repeatedly do. Excellence, then, is not an act but a habit",
author: "Aristotle",
},
];
const queryParams = (req) => {
const searchParams = new URL(req.url).searchParams;
return Object.fromEntries(searchParams.entries());
};
// Extract the date from the request, or default to today
const { date: requestedDate } = queryParams(request) ||
new Date().toISOString().split("T")[0];
const currentDate = new Date().toISOString().split("T")[0];
// Ensure the requested date is not in the future
if (requestedDate > currentDate) {
return new Response(
JSON.stringify({
error: "Cannot retrieve quotes from the future.",
}),
{
headers: {
"Content-Type": "application/json",
},
},
);
}
// Find the quote for the requested date
const selectedQuote = quotes.find((q) => q.date === requestedDate);
if (selectedQuote) {
// Return the selected quote as JSON
return new Response(JSON.stringify(selectedQuote), {
headers: {
"Content-Type": "application/json",
},
});
}
else {
// Handle case where no quote is found for the requested date
return new Response(
JSON.stringify({
error: `No quote available for the date ${requestedDate}.`,
}),
{
headers: {
"Content-Type": "application/json",
},
},
);
}
};
boris-quoteresponder.web.val.run
October 23, 2023