replace 4xx and 5xx with _error, per 0.14

This commit is contained in:
Rich Harris
2018-06-28 11:48:18 -04:00
parent 7f515c236c
commit fa460a2989
4 changed files with 22 additions and 37 deletions

View File

@@ -46,14 +46,17 @@
<script>
export default {
preload({ params, query }) {
// the `slug` parameter is available because this file
// is called [slug].html
const { slug } = params;
async preload({ params, query }) {
// the `slug` parameter is available because
// this file is called [slug].html
const res = await this.fetch(`blog/${params.slug}.json`);
const data = await res.json();
return this.fetch(`blog/${slug}.json`).then(r => r.json()).then(post => {
return { post };
});
if (res.status === 200) {
return { post: data };
} else {
this.error(res.status, data.message);
}
}
};
</script>

View File

@@ -6,8 +6,8 @@ posts.forEach(post => {
});
export function get(req, res, next) {
// the `slug` parameter is available because this file
// is called [slug].json.js
// the `slug` parameter is available because
// this file is called [slug].json.js
const { slug } = req.params;
if (lookup.has(slug)) {
@@ -17,6 +17,12 @@ export function get(req, res, next) {
res.end(lookup.get(slug));
} else {
next();
res.writeHead(404, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({
message: `Not found`
}));
}
}