From c1d460f2b88a102be05ddeb04af57db90c9f92bf Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Thu, 13 Aug 2020 10:38:12 -0500 Subject: Remove client JS requirement from index --- src/server.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- src/templates.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 src/templates.js (limited to 'src') diff --git a/src/server.js b/src/server.js index 92146e3..a419c74 100644 --- a/src/server.js +++ b/src/server.js @@ -7,6 +7,8 @@ const uuidv4 = require('uuid/v4'); const path = require('path'); const rss = require('rss'); +const templates = require('./templates'); + const Op = require('sequelize').Op; const multer = require('multer'); @@ -47,6 +49,39 @@ function hashWithSalt(password, salt){ return hash.digest("base64"); }; +async function constructFeed(models, postType){ + var posts = await models.posts.findAll({ + where: { type: postType }, order: [['createdAt', 'DESC']] + }); + posts = posts.map(x => x.get({ plain: true })); + await addImagesAndTagsToPosts(models, posts) + + var html = [] + html.push(`
`) + posts.forEach(post => { + html.push(`
+

${post.description}

+
`) + post.images.forEach(image => { + html.push(` + + `) + }) + html.push(`
+

+ ${post.createdAt.toString().substring(0,10)}`) + post.tags.forEach(tag => { + html.push(` + ${tag} + `) + }) + html.push(`

+
`) + }) + html.push(`
`) + return html.join(""); +} + function setUpRoutes(models, jwtFunctions, database) { // Authentication routine server.use(function (req, res, next) { @@ -88,8 +123,17 @@ function setUpRoutes(models, jwtFunctions, database) { next() }) - server.get('/', (req, res) => res.sendFile(__dirname + "/html/index.html")) - server.get('/index', (req, res) => res.sendFile(__dirname + "/html/index.html")) + server.get('/', async (req, res) => { + var html = [] + html.push(templates["index"]["pre"]) + html.push(templates["titlebar"]) + html.push(await constructFeed(models, "index")) + html.push(templates["footer"]) + html.push(templates["index"]["post"]) + + res.status(200).send(html.join("")) + // res.sendFile(__dirname + "/html/index.html") + }) server.get('/admin', (req, res) => res.sendFile(__dirname + "/html/admin.html")); server.get('/login', (req, res) => res.sendFile(__dirname + "/html/login.html")) server.get('/email', (req, res) => res.sendFile(__dirname + "/html/email.html")) @@ -151,7 +195,6 @@ function setUpRoutes(models, jwtFunctions, database) { res.sendFile(__dirname + "/html/post-single.html"); }) server.get('/tags/:name', async (req, res, next) => { - console.log("TAGS/NAME"); try { const { name } = req.params; const postsWithTag = await models.tags.findAll({ attributes: ["postId"], where: { text: name } }) @@ -163,7 +206,6 @@ function setUpRoutes(models, jwtFunctions, database) { }); posts = posts.map(x => x.get({ plain: true })); await addImagesAndTagsToPosts(models, posts) - console.log(posts); res.status(200).send(posts); next(); } catch (e) { diff --git a/src/templates.js b/src/templates.js new file mode 100644 index 0000000..b1cde30 --- /dev/null +++ b/src/templates.js @@ -0,0 +1,40 @@ +module.exports = { + titlebar: ``, + footer: ``, + + index: { + pre: ` + + + + Mark's Kitchen + + + + + + + +

Welcome to Mark's Kitchen

`, + post: ` + ` + } +} \ No newline at end of file -- cgit v1.2.3