mirror of
https://github.com/kevin-DL/sapper-template.git
synced 2026-01-23 15:01:27 +00:00
create blog posts from markdown files
This commit is contained in:
@@ -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'
|
||||
|
||||
Reference in New Issue
Block a user