diff options
author | Mark Powers <markppowers0@gmail.com> | 2020-08-13 10:38:12 -0500 |
---|---|---|
committer | Mark Powers <markppowers0@gmail.com> | 2020-08-13 10:38:12 -0500 |
commit | c1d460f2b88a102be05ddeb04af57db90c9f92bf (patch) | |
tree | 4dd25a8d376fa9ca9a4c9f8f5200a4ef353a3f41 | |
parent | 9796451829c63c4f1b5d81c1de49e2d88ffe592d (diff) |
Remove client JS requirement from index
-rw-r--r-- | src/server.js | 50 | ||||
-rw-r--r-- | src/templates.js | 40 |
2 files changed, 86 insertions, 4 deletions
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(`<div class="feed">`) + posts.forEach(post => { + html.push(`<div class="card"> + <p class="card-text">${post.description}</p> + <div class="card-img">`) + post.images.forEach(image => { + html.push(`<span> + <a href="/${image}"><img src="/${image}"></a> + </span>`) + }) + html.push(`</div> + <p class="date"> + <a href="/post/${post.type}/${post.id}">${post.createdAt.toString().substring(0,10)}</a>`) + post.tags.forEach(tag => { + html.push(`<span> + <a class="tag" href="/tags#${tag}">${tag}</a> + </span>`) + }) + html.push(`</p> + </div>`) + }) + html.push(`</div>`) + 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: `<nav class="titlebar"> + <div> + <a href="bread" class="btn btn-primary">Bread</a> + <a href="blog" class="btn btn-primary">Blog</a> + <!-- <a href="essay" class="btn btn-primary">Essays</a> (Hello inspector, this page exists, but just isn't very interesting, so I'm removing the link) --> + <a href="https://games.marks.kitchen" class="btn btn-primary">Games</a> + <a href="email" class="btn btn-primary">Email</a> + <a href="misc" class="btn btn-primary">Misc</a> +</div> +</nav>`, + footer: `<footer> + <div>Mark Powers</div> + <div> + <a href="/feed.xml">RSS feed</a> + <span class="spacer"></span> + <a href="https://github.com/Mark-Powers">GitHub</a> + <span class="spacer"></span> + <span>mark-at-marks.kitchen</span> + </div> +</footer>`, + + index: { + pre: `<!doctype html> + <html lang="en"> + + <head> + <title>Mark's Kitchen</title> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> + <link rel="stylesheet" type="text/css" href="/css/styles.css"> + <link rel="shortcut icon" href="/favicon.ico"> + </head> + + <body> + <h1>Welcome to Mark's Kitchen</h1>`, + post: `</body> + </html>` + } +}
\ No newline at end of file |