Created
November 7, 2021 22:43
-
-
Save szaranger/14014e1ac3923a24cff8d2f534a4c28c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import React from 'react'; | |
| import ReactDOMServer from 'react-dom/server'; | |
| import express from 'express'; | |
| const app = express(); | |
| app.use('/static', express.static('public')); | |
| const App = (props) => { | |
| return <div>Hello {props.name}</div>; | |
| }; | |
| const Html = (props) => { | |
| return ( | |
| <html> | |
| <body> | |
| <div id="app">{props.children}</div> | |
| <script id="initial-data" type="text/plain" data-json={props.initialData}></script> | |
| <script src="/static/bundle.js"></script> | |
| </body> | |
| </html> | |
| ); | |
| }; | |
| const initialData = { | |
| name: 'World' | |
| }; | |
| app.get('/', (req, res) => { | |
| ReactDOMServer.renderToNodeStream( | |
| <Html initialData={JSON.stringify(initialData)}> | |
| <App {...initialData} name="World!" /> | |
| </Html> | |
| ).pipe(res); | |
| }); | |
| app.listen(8080, () => { | |
| console.log('listening on port 8080...'); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment