-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
127 lines (110 loc) · 3.17 KB
/
server.js
File metadata and controls
127 lines (110 loc) · 3.17 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import "dotenv/config";
import express from "express";
import axios from "axios";
const app = express();
const PORT = process.env.PORT || 3000;
const quotesBaseURL = "https://api.api-ninjas.com/v2/randomquotes";
const quotesAPIKey = process.env.QUOTES_API_KEY;
let blogPosts = [];
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.get("/", async (req, res) => {
try {
const response = await axios.get(quotesBaseURL, {
headers: { "X-Api-Key": quotesAPIKey },
});
res.render("index.ejs", { quoteData: response.data[0] });
} catch (error) {
res.render("index.ejs", {
quoteData: { quote: "Coffee first.", author: "System" },
});
}
});
app.get("/blog", (req, res) => {
res.render("blog.ejs", {
posts: blogPosts,
quoteData: { quote: "The journal of a developer.", author: "The Grnd" },
});
});
app.get("/post", async (req, res) => {
try {
const response = await axios.get(quotesBaseURL, {
headers: { "X-Api-Key": quotesAPIKey },
});
res.render("post.ejs", { quoteData: response.data[0] });
} catch (error) {
res.render("post.ejs", {
quoteData: { quote: "What's brewing today?", author: "The Grnd" },
});
}
});
app.get("/post/:slug", (req, res) => {
const foundPost = blogPosts.find((p) => p.slug === req.params.slug);
if (foundPost) {
res.render("full-post.ejs", {
post: foundPost,
quoteData: {
quote: "Immerse yourself in the story.",
author: "The Grnd",
},
});
} else {
res.redirect("/blog");
}
});
app.post("/post", (req, res) => {
const randomSeed = Math.floor(Math.random() * 1000);
const postSlug = req.body.title
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, "")
.replace(/[\s_-]+/g, "-")
.replace(/^-+|-+$/g, "");
const newEntry = {
title: req.body.title,
date: new Date().toLocaleDateString("en-US", {
month: "short",
day: "numeric",
year: "numeric",
}),
category: req.body.category || "Journal",
excerpt: req.body.excerpt,
image:
req.body.image && req.body.image.startsWith("http")
? req.body.image
: `https://picsum.photos/seed/${randomSeed}/800/600`,
slug: postSlug || `entry-${randomSeed}`,
};
blogPosts.unshift(newEntry);
res.redirect("/blog");
});
app.get("/edit/:slug", (req, res) => {
const post = blogPosts.find((p) => p.slug === req.params.slug);
res.render("edit-post.ejs", {
post,
quoteData: { quote: "Refining the blend." },
});
});
app.post("/edit/:slug", (req, res) => {
const index = blogPosts.findIndex((p) => p.slug === req.params.slug);
if (index !== -1) {
blogPosts[index] = {
...blogPosts[index],
title: req.body.title,
excerpt: req.body.excerpt,
};
}
res.redirect(`/post/${req.params.slug}`);
});
app.post("/delete/:slug", (req, res) => {
blogPosts = blogPosts.filter((p) => p.slug !== req.params.slug);
res.redirect("/blog");
});
app.get("/contact", (req, res) => {
res.render("contact.ejs", {
quoteData: { quote: "Let's collaborate over coffee.", author: "The Grnd" },
});
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});