Commit f846d5be authored by Bruno Boiget's avatar Bruno Boiget
Browse files

make blog list updateable

parent 840bfcd8
import posts from './_posts.js';
import { posts } from './_posts.js';
const lookup = new Map();
posts.forEach(post => {
lookup.set(post.slug, JSON.stringify(post));
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;
// the `slug` parameter is available because
// this file is called [slug].json.js
const { slug } = req.params;
const blogPost = posts.find((p) => p.slug === slug);
if (lookup.has(slug)) {
res.writeHead(200, {
'Content-Type': 'application/json'
});
if (blogPost) {
res.writeHead(200, {
'Content-Type': 'application/json',
});
res.end(lookup.get(slug));
} else {
res.writeHead(404, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify(blogPost));
} else {
res.writeHead(404, {
'Content-Type': 'application/json',
});
res.end(JSON.stringify({
message: `Not found`
}));
}
res.end(
JSON.stringify({
message: `Not found`,
})
);
}
}
......@@ -7,7 +7,7 @@
// we don't want to create an `/blog/posts` route — the leading
// underscore tells Sapper not to do that.
const posts = [
export let posts = [
{
title: 'Hello Dijon',
slug: 'hello-dijon',
......@@ -96,5 +96,3 @@ const posts = [
posts.forEach((post) => {
post.html = post.html.replace(/^\t{3}/gm, '');
});
export default posts;
import posts from './_posts.js';
import { posts } from './_posts.js';
const contents = JSON.stringify(
posts.map((post) => {
......@@ -13,7 +13,7 @@ export function get(req, res) {
res.writeHead(200, {
'Content-Type': 'application/json',
});
res.end(contents);
res.end(JSON.stringify(posts));
}
export function post(req, res) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment