diff options
Diffstat (limited to 'routes/home.js')
| -rw-r--r-- | routes/home.js | 40 |
1 files changed, 40 insertions, 0 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; |
