Rework API

This commit is contained in:
LordMathis 2017-11-09 21:51:04 +01:00
parent ad90ce6789
commit 8ab9bb11ea
5 changed files with 43 additions and 33 deletions

View File

@ -3,7 +3,11 @@ import React, {Component} from 'react';
export default class Post extends Component {
render() {
return (
<div>Hello</div>
<div>
<div className="content">
<h1>Post</h1>
</div>
</div>
)
}
}

View File

@ -11,10 +11,22 @@ export default class PostContainer extends Component {
};
}
componentDidMount() {
const url = '/api/post/' + this.props.match.params.postname;
axios.get(url).then((res) => {
console.log(res.data);
this.setState({
isLoading: false,
post: res.data,
});
})
}
render() {
return (
<Post isLoading={this.state.isLoading}
post={this.state.post} />
)
);
}
}

View File

@ -31,20 +31,19 @@ if (process.env.NODE_ENV === 'development') {
app.use(hotDevMiddleware(compiler))
}
app.use(require('express').static('public'))
var api = require('./utils/api');
api(app);
app.use(require('express').static('public'));
require('./utils/scanner')();
var serverRender = require('./utils/serverRender')
app.get("*", serverRender)
var api = require('./utils/api');
app.use("/api", api);
var serverRender = require('./utils/serverRender');
app.get("*", serverRender);
app.listen(port, function(error) {
if (error) {
console.error(error)
console.error(error);
} else {
console.info("[Server] Listening on port %s", port)
console.info("[Server] Listening on port %s", port);
}
})

View File

@ -1,11 +1,11 @@
const data = require('./data.json');
const api = require('express').Router();
module.exports = function(app) {
app.get('/api/blog', (req, res) => {
api.get('/blog', (req, res) => {
res.json(data.posts);
});
});
app.get('api/post/:postname', (req, res) => {
api.get('/post/:postname', (req, res) => {
const post = data.posts.find((el) => {
el.filename === req.params.postname
});
@ -17,9 +17,7 @@ module.exports = function(app) {
error: 404
});
}
})
}
});
function (postname) {
data.posts
}
module.exports = api;

View File

@ -4,9 +4,6 @@ import { renderToString } from 'react-dom/server'
import { StaticRouter as Router } from 'react-router-dom'
import { App } from '../components/App'
function serverRender(req, res) {
let markup = '';
let status = 200;
@ -40,4 +37,4 @@ function renderFullPage(html) {
`
}
module.exports = serverRender
module.exports = serverRender;