diff options
author | Mark Powers <mark@marks.kitchen> | 2021-09-02 19:23:11 -0500 |
---|---|---|
committer | Mark Powers <mark@marks.kitchen> | 2021-09-02 19:23:11 -0500 |
commit | 524e521ad3548934c4effa104eb4ac1299a1816a (patch) | |
tree | 641296819bde9fb31c9c9f566c4905b564e81a81 /src/server.js | |
parent | d5f2c72acd877acbb3683b50cfbbe9f3c0d98af3 (diff) |
Add function to generate sitemap
Diffstat (limited to 'src/server.js')
-rw-r--r-- | src/server.js | 41 |
1 files changed, 38 insertions, 3 deletions
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 `<url><loc>${host}${path}</loc><lastmod>${updated}</lastmod></url>` +} + +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>${urlset.join("")}</urlset>`; +} + 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 }); |