Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Last active April 20, 2021 23:15
Show Gist options
  • Save DavidWells/460e9dfbc5cce0f9019e to your computer and use it in GitHub Desktop.
Save DavidWells/460e9dfbc5cce0f9019e to your computer and use it in GitHub Desktop.
Serverside React Rendering
<!doctype html>
<html>
<head>
<title>React Isomorphic Server Side Rendering Example</title>
<link href='/styles.css' rel="stylesheet">
</head>
<body>
<h1 id="main-title">Isomorphic Server Side Rendering with React</h1>
<div id="react-main-mount">
<%- reactOutput %>
</div>
<!-- comment out main.js to see server side only rendering -->
<script src="/main.js"></script>
</body>
</html>
var React = require('react'),
ReactApp = React.createFactory(require('../components/ReactApp'));
module.exports = function(app) {
app.get('/', function(req, res){
// React.renderToString takes your component and generates the markup
var reactHtml = React.renderToString(ReactApp({}));
// Output html rendered by react into .ejs file. Can be any template
res.render('index.ejs', {reactOutput: reactHtml});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment