Last active
April 20, 2021 23:15
-
-
Save DavidWells/460e9dfbc5cce0f9019e to your computer and use it in GitHub Desktop.
Serverside React Rendering
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
<!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> |
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
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