Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save szaranger/14014e1ac3923a24cff8d2f534a4c28c to your computer and use it in GitHub Desktop.
Save szaranger/14014e1ac3923a24cff8d2f534a4c28c to your computer and use it in GitHub Desktop.
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