made sitemap and rss only be generated on change

This commit is contained in:
Ellpeck 2019-10-05 01:07:38 +02:00
parent bb5d42ef16
commit 71916a7f6f
3 changed files with 20 additions and 27 deletions

View file

@ -7,25 +7,18 @@ const converter = new showdown.Converter({
parseImgDimensions: true parseImgDimensions: true
}); });
module.exports = function (app) { module.exports = function () {
app.get('/rss.xml', function (_req, res) { let folder = __dirname + "/../";
fs.watchFile(folder + "blog/posts.json", function (curr, prev) {
if (curr.mtime == prev.mtime)
return;
console.log("Refreshing feeds...");
createFeed(function (feed) { createFeed(function (feed) {
res.header('Content-Type', 'application/xml'); fs.writeFile(folder + "feed.json", feed.json1(), function (_, _) {});
res.send(feed.rss2()); fs.writeFile(folder + "rss.xml", feed.rss2(), function (_, _) {});
fs.writeFile(folder + "atom.xml", feed.atom1(), function (_, _) {});
}); });
}); });
app.get('/feed.json', function (_req, res) {
createFeed(function (feed) {
res.header('Content-Type', 'application/json');
res.send(feed.json1());
});
});
app.get('/atom.xml', function (_req, res) {
createFeed(function (feed) {
res.header('Content-Type', 'application/xml');
res.send(feed.atom1());
});
})
} }
function createFeed(callback) { function createFeed(callback) {

View file

@ -1,6 +1,2 @@
const express = require("express"); require("./rss")();
require("./sitemap")();
let app = express();
require("./rss")(app);
require("./sitemap")(app);
app.listen(3000);

View file

@ -3,8 +3,13 @@ const {
} = require('sitemap'); } = require('sitemap');
const fs = require("fs"); const fs = require("fs");
module.exports = function (app) { module.exports = function () {
app.get('/sitemap.xml', function (_req, res) { let folder = __dirname + "/../";
fs.watchFile(folder + "blog/posts.json", function (curr, prev) {
if (curr.mtime == prev.mtime)
return;
console.log("Refreshing sitemap...");
let sitemap = createSitemap({ let sitemap = createSitemap({
hostname: 'https://ellpeck.de', hostname: 'https://ellpeck.de',
urls: [{ urls: [{
@ -31,7 +36,7 @@ module.exports = function (app) {
] ]
}); });
fs.readFile(__dirname + "/../blog/posts.json", function (_, data) { fs.readFile(folder + "blog/posts.json", function (_, data) {
let json = JSON.parse(data); let json = JSON.parse(data);
for (let post of json) { for (let post of json) {
@ -41,8 +46,7 @@ module.exports = function (app) {
}); });
} }
res.header('Content-Type', 'application/xml'); fs.writeFile(folder + "/sitemap.xml", sitemap.toXML(), function (_, _) {});
res.send(sitemap.toXML());
}); });
}); });
} }