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
});
module.exports = function (app) {
app.get('/rss.xml', function (_req, res) {
module.exports = function () {
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) {
res.header('Content-Type', 'application/xml');
res.send(feed.rss2());
fs.writeFile(folder + "feed.json", feed.json1(), function (_, _) {});
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) {

View file

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

View file

@ -3,8 +3,13 @@ const {
} = require('sitemap');
const fs = require("fs");
module.exports = function (app) {
app.get('/sitemap.xml', function (_req, res) {
module.exports = function () {
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({
hostname: 'https://ellpeck.de',
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);
for (let post of json) {
@ -41,8 +46,7 @@ module.exports = function (app) {
});
}
res.header('Content-Type', 'application/xml');
res.send(sitemap.toXML());
fs.writeFile(folder + "/sitemap.xml", sitemap.toXML(), function (_, _) {});
});
});
}