Swap /urls for /rss; complete RSS feed

master
Raymonzut 4 years ago
parent da9024548d
commit b4440c8cc9
No known key found for this signature in database
GPG Key ID: 1E9BCC39EDD1DD53
  1. 33
      server/routes/api/posts.js

@ -56,9 +56,9 @@ routes.set('/:id', async (req, res) => {
else res.send(results[0]) else res.send(results[0])
}) })
// For the sitemap.xml, will be listed in the XML Sitemap Index // Dynamic RSS feed
routes.set('/urls', async (req, res) => { routes.set('/rss.xml', async (req, res) => {
const re = /(\/api\/)?(.*)\/urls/g const re = /(\/api\/)?(.*)\/rss/g
const result = [...req.raw.originalUrl.matchAll(re)] const result = [...req.raw.originalUrl.matchAll(re)]
if (!result) { if (!result) {
notFoundResponse(res) notFoundResponse(res)
@ -67,17 +67,24 @@ routes.set('/urls', async (req, res) => {
const endpoint = result [1] ? result[1][2] : result[0][2] const endpoint = result [1] ? result[1][2] : result[0][2]
// Assumes https // Assumes https
const BASE_URL = 'https://' + req.raw.hostname + endpoint const BASE_URL = 'https://' + req.raw.hostname + endpoint
const urls = posts.map(post => const ITEMS = posts.map(post =>
`\n <url> `\r\n <item>
<loc>${BASE_URL}?post=${post._id}</loc> <title>${post.title}</title>
<lastmod>${post.date}</lastmod> <link>https://${req.raw.hostname}/posts?post=${post._id}</link>
<changefreq>never</changefreq> <guid>https://${req.raw.hostname}/posts?post=${post._id}</guid>
<priority>0.9</priority> <description>${post.content}</description>
</url>` </item>`
).join("\n") ).join("\n")
const xml = `<?xml version="1.0" encoding="UTF-8"?>\r<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> const xml = `<?xml version="1.0" encoding="UTF-8" ?>\n<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
${urls}\n\r</urlset>` <channel>
<atom:link href="https://${req.raw.hostname}/api/posts/rss.xml" rel="self" type="application/rss+xml" />
<title>Posts</title>
<link>https://${req.raw.hostname}</link>
<description>Personal blog</description>
<language>en-us</language>
${ITEMS}
</channel>\n</rss>`
res.header('Content-Type', 'application/xml') res.header('Content-Type', 'application/xml')
res.status(200).send(xml) res.status(200).send(xml)

Loading…
Cancel
Save