diff --git a/src/components/Post.js b/src/components/Post.js
index 01424ae..0dee37d 100644
--- a/src/components/Post.js
+++ b/src/components/Post.js
@@ -3,7 +3,11 @@ import React, {Component} from 'react';
export default class Post extends Component {
render() {
return (
-
Hello
+
)
}
}
diff --git a/src/containers/PostContainer.js b/src/containers/PostContainer.js
index 18b0678..0de2a79 100644
--- a/src/containers/PostContainer.js
+++ b/src/containers/PostContainer.js
@@ -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 (
- )
+ );
}
}
diff --git a/src/server.js b/src/server.js
index eca2304..7c5b50d 100644
--- a/src/server.js
+++ b/src/server.js
@@ -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);
}
})
diff --git a/src/utils/api.js b/src/utils/api.js
index 2b2d497..12c249e 100644
--- a/src/utils/api.js
+++ b/src/utils/api.js
@@ -1,25 +1,23 @@
const data = require('./data.json');
+const api = require('express').Router();
-module.exports = function(app) {
- app.get('/api/blog', (req, res) => {
- res.json(data.posts);
+api.get('/blog', (req, res) => {
+ res.json(data.posts);
+});
+
+api.get('/post/:postname', (req, res) => {
+ const post = data.posts.find((el) => {
+ el.filename === req.params.postname
});
- app.get('api/post/:postname', (req, res) => {
- const post = data.posts.find((el) => {
- el.filename === req.params.postname
+ if (post) {
+ res.json(post);
+ } else {
+ res.json({
+ error: 404
});
+ }
+});
- if (post) {
- res.json(post);
- } else {
- res.json({
- error: 404
- });
- }
- })
-}
-function (postname) {
- data.posts
-}
+module.exports = api;
diff --git a/src/utils/serverRender.js b/src/utils/serverRender.js
index dfb1684..07353b1 100644
--- a/src/utils/serverRender.js
+++ b/src/utils/serverRender.js
@@ -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;