From 524e521ad3548934c4effa104eb4ac1299a1816a Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Thu, 2 Sep 2021 19:23:11 -0500 Subject: Add function to generate sitemap --- src/server.js | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'src/server.js') diff --git a/src/server.js b/src/server.js index c770304..478e601 100644 --- a/src/server.js +++ b/src/server.js @@ -58,7 +58,7 @@ function formatDate(d) { async function formatPostsforSingle(models, postType, postId){ var posts = await models.posts.findAll({ - where: { + where: { type: postType, id: postId }, order: [['createdAt', 'DESC']] @@ -85,6 +85,36 @@ async function formatPostsForType(models, postType){ return posts; } +function to_sitemap_xml(host, path, updated){ + return `${host}${path}${updated}` +} + +async function sitemap(models) { + urlset = [] + host = "https://marks.kitchen" + let routes = [ + "/", + "/bread", + "/blog", + "/email", + "/work-square", + "/misc", + "/projects", + ] + routes.forEach(item => { + urlset.push(to_sitemap_xml(host, item, Date.now())) + }) + let posts = (await models.posts.findAll()).map(x => x.get({ plain: true })); + posts.forEach(post => { + urlset.push(to_sitemap_xml(host, `/posts/${post.type}/${post.id}`, posts.updatedAt)) + }) + let tags = (await models.tags.findAll()).map(x => x.get({ plain: true })); + tags.forEach(tag => { + urlset.push(to_sitemap_xml(host, `/tags/${tag.text}`, tag.updatedAt)) + }) + return `${urlset.join("")}`; +} + function setUpRoutes(models, jwtFunctions, database, templates) { // Authentication routine server.use(function (req, res, next) { @@ -174,8 +204,7 @@ function setUpRoutes(models, jwtFunctions, database, templates) { let body = templates["tags"]({posts, name}) res.status(200).send(body) }) - - server.get('/sitemap.xml', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/misc/sitemap.xml")); + server.get('/robots.txt', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/misc/robots.txt")); server.get('/admin', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/html/admin.html")); server.get('/login', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/html/login.html")) @@ -199,6 +228,11 @@ function setUpRoutes(models, jwtFunctions, database, templates) { res.status(200).send(body) }) + server.get('/sitemap.xml', cache('1 day'), async (req, res) => { + res.setHeader('Content-Type', 'text/xml') + res.status(200).send(await sitemap(models)) + }); + server.get('/wordsquares/best', cache('5 minutes'), async (req, res, next) => { var best = await database.query("select words, name from wordsquares where best = 1", { type: database.QueryTypes.SELECT }) res.status(200).send({ best: best }); @@ -269,6 +303,7 @@ function setUpRoutes(models, jwtFunctions, database, templates) { const type = req.body.type req.body.description = marked(req.body.description) req.body.likes = 0 + req.body.sent_email = false const newPost = await models.posts.create(req.body); req.files.forEach(async (file) => { await models.pictures.create({ "source": "uploads/" + file.filename, "postId": newPost.id }); -- cgit v1.2.3