From 7de275920bdd6115017b258ac78e62222c000dc3 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Tue, 3 Aug 2021 23:16:09 +0430 Subject: robots.txt and xml sitemap.WIP --- server.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'server.js') diff --git a/server.js b/server.js index a9f2973..b51bb52 100755 --- a/server.js +++ b/server.js @@ -2,6 +2,7 @@ "use strict"; const express = require("express"); +const sitemap = require("express-sitemap-xml"); const path = require("path"); const fs = require("fs"); const mit = require("markdown-it")({ html: true }) @@ -37,6 +38,7 @@ function renderAndSend(req, res) { path.join(__dirname, viewPath), "utf-8" ); + //FIXME-this can obviously fail readStream.on("data", (chunk) => { res.render("index", { cache: true, @@ -51,6 +53,21 @@ function renderAndSend(req, res) { } } +app.get("/health", (req, res) => { + res.type("application/json"); + let response = { isOK: "True", error: "" }; + res.send(response); +}); + +app.get("/robots.txt", (req, res) => { + res.type("text/plain"); + let robots_txt = "Sitemap: http://blog.terminaldweller.com\n"; + robots_txt += "User-agent: *\n"; + robots_txt += "Disallow: \n"; + robots_txt += "Crawl-Delay: 20"; + res.send(robots_txt); +}); + app.get("/$", (req, res) => { renderAndSend(req, res); }); @@ -62,4 +79,18 @@ app.get("/mds/:mdname$", (req, res) => { renderAndSend(req, res); }); +async function enumerateDir() { + return await fs.readdirSync(path.join(__dirname, "mds")); +} + +app.use(sitemap(enumerateDir, "http://blog.terminaldweller.com")); + +app.use((req, res) => { + return res.status(404).send({ message: "Path" + req.url + "not found!" }); +}); + +app.use((err, req, res) => { + return res.status(500).send({ error: err }); +}); + app.listen(9000); -- cgit v1.2.3