Add "Dragon Trainer Monthly" project

This commit is contained in:
Andrea Fazzi 2024-08-08 06:43:48 +02:00
parent b09f2f2c78
commit 1f700ca1ea
4 changed files with 59 additions and 0 deletions

View file

@ -0,0 +1,10 @@
# Random Ron
A site that displays a random Ron Swanson quote (from the show Parks
and Recreation) using the [Ron Swanson Quotes
API](https://github.com/jamesseanwright/ron-swanson-quotes).
# Reference
* https://leanwebclub.com/learn/js-essentials/project-random-ron

View file

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
<title>Dragon Trainer Monthly</title>
</head>
<body>
<h1>Dragon Trainer Monthly</h1>
<div id="app"></div>
<script src="script.js"></script>
</body>
</html>

View file

@ -0,0 +1,34 @@
let app = document.querySelector('#app');
let endpoint = 'https://vanillajsacademy.com/api/dragons.json';
async function getArticles() {
app.innerHTML = 'Fetching articles...';
try {
let response = await fetch(endpoint);
if (!response.ok) throw response.status;
let dragons = await response.json();
if (!dragons) throw 'no data';
app.innerHTML = dragons.articles.map(function(item) {
return `
<h2>${item.title}</h2>
<cite>${item.pubdate} by <strong>${item.author}</strong></cite>
<p>${item.article}</p>
<hr/>
`
}).join('');
} catch (error) {
console.warn(error);
}
}
getArticles();

View file

@ -17,6 +17,7 @@ func main() {
mux.Handle("GET /random-ron/", http.StripPrefix("/random-ron", http.FileServer(http.Dir("random-ron")))) mux.Handle("GET /random-ron/", http.StripPrefix("/random-ron", http.FileServer(http.Dir("random-ron"))))
mux.Handle("GET /random-ron-no-dup/", http.StripPrefix("/random-ron-no-dup", http.FileServer(http.Dir("random-ron-no-dup")))) mux.Handle("GET /random-ron-no-dup/", http.StripPrefix("/random-ron-no-dup", http.FileServer(http.Dir("random-ron-no-dup"))))
mux.Handle("GET /random-ron-async-await/", http.StripPrefix("/random-ron-async-await", http.FileServer(http.Dir("random-ron-async-await")))) mux.Handle("GET /random-ron-async-await/", http.StripPrefix("/random-ron-async-await", http.FileServer(http.Dir("random-ron-async-await"))))
mux.Handle("GET /dragon-trainer-monthly/", http.StripPrefix("/dragon-trainer-monthly", http.FileServer(http.Dir("dragon-trainer-monthly"))))
log.Println("Start the web server...") log.Println("Start the web server...")
err := http.ListenAndServe(":8080", mux) err := http.ListenAndServe(":8080", mux)