Post API is fully working

This commit is contained in:
LordMathis 2017-11-11 20:00:36 +01:00
parent 8ab9bb11ea
commit 9bc95d9670
3 changed files with 34 additions and 18 deletions

View File

@ -31,15 +31,13 @@ export default class Blog extends Component {
)
return (
<div>
<div className="content">
<h1>Blog</h1>
<div className="blog-posts">
{posts}
</div>
<div className="content">
<h1>Blog</h1>
<div className="blog-posts">
{posts}
</div>
</div>
);
}

View File

@ -2,10 +2,18 @@ import React, {Component} from 'react';
export default class Post extends Component {
render() {
if (this.props.isLoading) {
return (
<h1>Loading</h1>
);
}
return (
<div>
<div className="content">
<h1>Post</h1>
<div className="content">
<h1>{this.props.post.title}</h1>
<h4>{this.props.post.published}</h4>
<div dangerouslySetInnerHTML={{__html: this.props.post.body}}>
</div>
</div>
)

View File

@ -1,22 +1,32 @@
const data = require('./data.json');
const api = require('express').Router();
const fs = require('fs');
const path = require('path');
api.get('/blog', (req, res) => {
res.json(data.posts);
});
api.get('/post/:postname', (req, res) => {
const postname = req.params.postname;
const post = data.posts.find((el) => {
el.filename === req.params.postname
return el.filename === postname
});
if (post) {
res.json(post);
} else {
res.json({
error: 404
});
}
const renderPath = path.join(process.cwd(), '/renders', postname + '.html');
fs.readFile(renderPath, 'utf8', (err, data) => {
if (err) {
res.json({
error: 404
});
} else {
res.json({
published: post.published,
title: post.title,
body: data,
});
}
});
});