File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -18,12 +18,16 @@ server.use(sassMiddleware({
1818server . set ( 'view engine' , 'ejs' ) ;
1919
2020
21- import './serverRender' ;
21+ import serverRender from './serverRender' ;
2222
2323server . get ( '/' , ( req , res ) => {
24- res . render ( 'index' , {
25- content : '...'
26- } ) ;
24+ serverRender ( )
25+ . then ( content => {
26+ res . render ( 'index' , {
27+ content
28+ } ) ;
29+ } )
30+ . catch ( console . error ) ;
2731} ) ;
2832
2933server . use ( '/api' , apiRouter ) ;
Original file line number Diff line number Diff line change 1- import config from './config' ;
1+ import React from 'react' ;
2+ import ReactDOMServer from 'react-dom/server' ;
23import axios from 'axios' ;
34
4- axios . get ( `${ config . serverUrl } /api/contests` )
5- . then ( resp => {
6- console . log ( resp . data ) ;
7- } ) ;
5+ import App from './src/components/App' ;
6+ import config from './config' ;
87
8+ const serverRender = ( ) =>
9+ axios . get ( `${ config . serverUrl } /api/contests` )
10+ . then ( resp => {
11+ return ReactDOMServer . renderToString (
12+ < App initialContests = { resp . data . contests } />
13+ ) ;
14+ } ) ;
915
16+ export default serverRender ;
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ import ContestPreview from './ContestPreview';
66class App extends React . Component {
77 state = {
88 pageHeader : 'Naming Contests' ,
9- contests : [ ]
9+ contests : this . props . initialContests
1010 } ;
1111 componentDidMount ( ) {
1212 // Make Ajax call and load data response onto the state
Original file line number Diff line number Diff line change @@ -4,6 +4,6 @@ import ReactDOM from 'react-dom';
44import App from './components/App' ;
55
66ReactDOM . render (
7- < App /> ,
7+ < App initialContests = { [ ] } /> ,
88 document . getElementById ( 'root' )
99) ;
You can’t perform that action at this time.
0 commit comments