create blog posts from markdown files

This commit is contained in:
Rich Harris
2018-07-27 14:28:32 -04:00
parent 29e6481559
commit 0fa6c9f7d7
10 changed files with 129 additions and 98 deletions

View File

@@ -1,21 +1,23 @@
import posts from './_posts.js';
import { getPost } from './_posts.js';
const lookup = new Map();
posts.forEach(post => {
lookup.set(post.slug, JSON.stringify(post));
});
export function get(req, res, next) {
// the `slug` parameter is available because
// this file is called [slug].json.js
const { slug } = req.params;
if (lookup.has(slug)) {
if (process.env.NODE_ENV !== 'production' || !lookup.has(slug)) {
const post = getPost(slug);
lookup.set(slug, JSON.stringify(post));
}
const json = lookup.get(slug);
if (json) {
res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(lookup.get(slug));
res.end(json);
} else {
res.writeHead(404, {
'Content-Type': 'application/json'