mirror of
https://github.com/kevin-DL/sapper-template.git
synced 2026-01-23 23:11:29 +00:00
switch to [slug].json.js server routes, instead of /api/...
This commit is contained in:
22
routes/blog/[slug].json.js
Normal file
22
routes/blog/[slug].json.js
Normal file
@@ -0,0 +1,22 @@
|
||||
import posts 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)) {
|
||||
res.set({
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
|
||||
res.end(lookup.get(slug));
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user