Learning Full-Stack JavaScript Development: MongoDB, Node and React
5. Rendering on the Server
019 Fetching data from a remote API
$ npm install --save axios
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import React from 'react';
import axios from 'axios';
import Header from './Header';
import ContestPreview from './ContestPreview';
class App extends React.Component {
constructor(props){
super(props);
this.state = {
pageHeader: 'Naming Contests',
contests: []
};
}
componentDidMount() {
axios.get('/api/contests')
.then(resp => {
this.setState({
contests: resp.data.contests
});
})
.catch(console.error);
}
componentWillUnmount() {
}
render() {
return (
<h2 className="App">
<Header message= {this.state.pageHeader} />
<div>
{this.state.contests.map(contest =>
<ContestPreview key={contest.id} {...contest} />
)}
</div>
</h2>
);
}
};
export default App;
http://localhost/api/contests/
020 Fetching data from the server side
serverRender.js
1
2
3
4
5
6
7
import config from './config';
import axios from 'axios';
axios.get(`${config.serverUrl}/api/contests`)
.then(resp => {
console.log(resp.data);
});
021 Server rendering with ReactDOMServer
serverRender.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import App from './src/components/App';
import config from './config';
import axios from 'axios';
const serverRender = () =>
axios.get(`${config.serverUrl}/api/contests`)
.then(resp => {
// console.log(resp.data);
return ReactDOMServer.renderToString(
<App initialContests={resp.data.contests} />
);
});
export default serverRender;
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import config from './config';
import apiRouter from './api';
import sassMiddleware from 'node-sass-middleware';
import path from 'path';
import express from 'express';
const server = express();
server.use(sassMiddleware({
src: path.join(**dirname, 'sass'),
dest: path.join(**dirname, 'public')
}));
server.set('view engine', 'ejs');
server.get('/', (req, res) => {
serverRender()
.then(content => {
res.render('index', {
content
});
})
.catch(console.error);
});
import serverRender from './serverRender';
server.get('/about.html', (req, res) => {
res.send('The about page');
});
server.use('/api', apiRouter);
server.use(express.static('public'));
server.listen(config.port, config.host, () => {
console.info('Express listening on port ', config.port);
});
022 Fix the checksum problem
index.ejs
1
2
3
4
5
6
7
<script type="text/javascript">
window.initialData = <%- JSON.stringify(initialData) -%>
</script>
<%- include('header') -%>
<div id="root"><%- initialMarkup -%></div>
<%- include('footer') -%>
index.js
1
2
3
4
5
6
7
8
9
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
ReactDOM.render(
<App initialContests={window.initialData.contests} />,
document.getElementById('root')
);
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import config from './config';
import apiRouter from './api';
import sassMiddleware from 'node-sass-middleware';
import path from 'path';
import express from 'express';
const server = express();
server.use(sassMiddleware({
src: path.join(**dirname, 'sass'),
dest: path.join(**dirname, 'public')
}));
server.set('view engine', 'ejs');
server.get('/', (req, res) => {
serverRender()
.then(( {initialMarkup, initialData}) => {
res.render('index', {
initialMarkup,
initialData
});
})
.catch(console.error);
});
import serverRender from './serverRender';
server.get('/about.html', (req, res) => {
res.send('The about page');
});
server.use('/api', apiRouter);
server.use(express.static('public'));
server.listen(config.port, config.host, () => {
console.info('Express listening on port ', config.port);
});
serverRender.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import App from './src/components/App';
import config from './config';
import axios from 'axios';
const serverRender = () =>
axios.get(`${config.serverUrl}/api/contests`)
.then(resp => {
return {
initialMarkup: ReactDOMServer.renderToString(
<App initialContests={resp.data.contests} />
),
initialData: resp.data
};
});
export default serverRender;