mirror of
https://github.com/kevin-DL/sapper-template.git
synced 2026-01-20 21:45:10 +00:00
31 lines
551 B
JavaScript
31 lines
551 B
JavaScript
import { getPost } from './_posts.js';
|
|
|
|
const lookup = new Map();
|
|
|
|
export function get(req, res, next) {
|
|
const { slug } = req.params;
|
|
|
|
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(json);
|
|
} else {
|
|
res.writeHead(404, {
|
|
'Content-Type': 'application/json'
|
|
});
|
|
|
|
res.end(JSON.stringify({
|
|
message: `Not found`
|
|
}));
|
|
}
|
|
}
|