aboutsummaryrefslogtreecommitdiff
path: root/routes
diff options
context:
space:
mode:
authorBobby <[email protected]>2022-03-21 00:56:05 -0400
committerBobby <[email protected]>2022-03-21 00:56:05 -0400
commitb38eaa1a814769d93b7cd8348804fc1241098189 (patch)
tree10f38f7725b8fdc027aa86d60d6a944b1c07ff52 /routes
parent8ca6ea34ae769c500069452e36e6056d59dc5ff9 (diff)
downloadluciferreeves.github.io-b38eaa1a814769d93b7cd8348804fc1241098189.tar.xz
luciferreeves.github.io-b38eaa1a814769d93b7cd8348804fc1241098189.zip
Server side rendering of index page
Diffstat (limited to 'routes')
-rw-r--r--routes/home.js40
-rw-r--r--routes/index.js8
2 files changed, 45 insertions, 3 deletions
diff --git a/routes/home.js b/routes/home.js
new file mode 100644
index 0000000..98309b6
--- /dev/null
+++ b/routes/home.js
@@ -0,0 +1,40 @@
+const express = require("express");
+const router = express.Router();
+const cheerio = require("cheerio");
+const firebase = require("../firebase");
+const fs = require("fs");
+
+router.get("/", (req, res) => {
+ const store = firebase.firestore();
+ const posts = [];
+ let query = store.collection("posts");
+ query = query.select("slug", "tags", "title", "shortText", "publishDate");
+ query
+ .get()
+ .then(function (querySnapshot) {
+ querySnapshot.forEach(function (doc) {
+ posts.push(doc.data());
+ });
+ })
+ .then(() => {
+ var html = fs.readFileSync(
+ __dirname + "/../public/views/index.html",
+ "utf8"
+ );
+ var $ = cheerio.load(html);
+ $("#posts").html("");
+ posts.forEach((post, index) => {
+ if (index === 0) {
+ $('#hero-unit').removeClass('hidden');
+ $('#hero-unit').html(`
+ <h1>${post.title}</h1>
+ <p>${post.shortText}</p>
+ <p><a class="btn btn-primary btn-lg" href="/posts/${post.slug}">Read More</a></p>
+ `);
+ }
+ });
+ res.send($.html());
+ });
+});
+
+module.exports = router;
diff --git a/routes/index.js b/routes/index.js
index 37799b1..659d50d 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -1,21 +1,23 @@
// Import express router
const express = require("express");
const router = express.Router();
+
// Import the routes
const admin = require("./admin");
const repositories = require("./repositories");
const blog = require("./blog");
const posts = require("./posts");
+const home = require("./home");
+
// Set the routes
router.use("/admin", admin);
router.use("/api/blog", blog);
router.use("/", repositories);
router.use("/", posts);
+router.use("/", home);
// Create the routes
-router.get("/", (req, res) => {
- res.render("index.html");
-});
+
router.get("/about", (req, res) => {
res.render("about.html");
});