From 56fffda0fe348da77b5df7f9270c14ded097a0aa Mon Sep 17 00:00:00 2001 From: Raymonzut <40148684+Raymonzut@users.noreply.github.com> Date: Tue, 19 May 2020 20:27:32 +0200 Subject: [PATCH] Route /urls: returns xml for posts --- server/routes/api/posts.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/server/routes/api/posts.js b/server/routes/api/posts.js index 835529a..6080231 100644 --- a/server/routes/api/posts.js +++ b/server/routes/api/posts.js @@ -56,4 +56,25 @@ routes.set('/:id', async (req, res) => { else res.send(results[0]) }) +// For the sitemap.xml, will be listed in the XML Sitemap Index +routes.set('/urls', async (req, res) => { + const re = /(\/api\/)?(.*)\/urls/g + const result = [...req.raw.originalUrl.matchAll(re)] + if (!result) notFoundResponse(res) + const endpoint = result [1] ? result[1][2] : result[0][2] + // Assumes https + const BASE_URL = 'https://' + req.raw.hostname + endpoint + const urls = posts.map(post => + `\n + ${BASE_URL}/${post._id} + ${post.date} + never + 0.9 + ` + ).join("\n") + const xml = `\r + ${urls}\n\r` + res.send(xml) +}) + module.exports = routes