-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Step 2: trivial server-side rendering with Express
RUN: `npm run start` and go to `http://localhost:3000`. - A little refactoring: move Hello component to separate file etc - Render the same Hello component server-side with ReactDOMServer Now we are rendering the same Hello component both on client and server: with express server we pre-render the Hello component on the server, and server the client with rendered html, and with webpack we continue to bundle client.js into ES5 code that the browser would understand, just as we did at previous step.
- Loading branch information
Showing
5 changed files
with
41 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React from 'react'; | ||
|
||
const Hello = React.createClass({ | ||
render: function() { | ||
return <div>Hello {this.props.name}</div>; | ||
} | ||
}); | ||
|
||
export default Hello; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import express from 'express'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import React from 'react'; | ||
import ReactDOMServer from 'react-dom/server'; | ||
import Hello from './Hello.js'; | ||
|
||
function handleRender(req, res) { | ||
const html = ReactDOMServer.renderToString( | ||
<Hello name="World" /> | ||
); | ||
fs.readFile('./index.html', 'utf8', function (err, file) { | ||
if (err) { | ||
return console.log(err); | ||
} | ||
const document = file.replace(/<div id="app"><\/div>/, `<div id="app">${html}</div>`); | ||
res.send(document); | ||
}); | ||
} | ||
|
||
const app = express(); | ||
// Serve built files with express static files middleware | ||
app.use('/built', express.static(path.join(__dirname, 'built'))); | ||
// Serve normal requests with our handleRender function | ||
app.get('*', handleRender); | ||
app.listen(3000); | ||
console.log('=== Go to http://localhost:3000 ==='); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters