Darek's thoughts on IT solutions

How to view remote markdown files in Chrome or Firefox

06.05.2020

Server script - serve-md.js:

const http = require("http"),
  fs = require("fs");
const params = require("./serve-md-params");
const PORT = params.PORT || 3000;
let file = process.argv[2] || "readme.md";
const server = http.createServer((req, res) => {
  //res.setHeader("Content-Type", "text/markdown; charset=UTF-8");
  if (req.url === "/") {
    res.writeHead(301, { Location: "readme.md" });
    res.end();
  } else {
    let content = fs.readFileSync(file, "utf-8");
    res.setHeader("Content-Type", "text/plain; charset=UTF-8");
    res.write(content);
    res.end();
  }
});

server.listen(PORT, () => {
  console.log(`listen to ${PORT}`);
});

serve-md-params.json is a file we require in the code, to get reserved port the server will listen at:

{
  "PORT": 3010
}

Usage: issue from command line:

# we assume it's 'readme.md' file in script's directory you want to preview
$ node serve-md
# OR to preview another file located in script's directory, say 'test.md':
$ node serve-md test.md

After that you need to open http://{DOMAIN_OR_IP}:{PORT} url in Chrome (Markdown Preview extension must be installed) or Firefox (here Markdown Viewer Webext likewise), say http://10.10.1.40:3010