Merge pull request #15 from mathiasbynens/map

Use a map for the lookup table
This commit is contained in:
Rich Harris
2017-12-25 08:42:41 -05:00
committed by GitHub

View File

@@ -1,8 +1,8 @@
import posts from './_posts.js';
const lookup = {};
const lookup = new Map();
posts.forEach(post => {
lookup[post.slug] = JSON.stringify(post);
lookup.set(post.slug, JSON.stringify(post));
});
export function get(req, res, next) {
@@ -10,14 +10,14 @@ export function get(req, res, next) {
// is called [slug].js
const { slug } = req.params;
if (slug in lookup) {
if (lookup.has(slug)) {
res.set({
'Content-Type': 'application/json',
'Cache-Control': `max-age=${30 * 60 * 1e3}` // cache for 30 minutes
});
res.end(lookup[slug]);
res.end(lookup.get(slug));
} else {
next();
}
}
}