Skip to content

Instantly share code, notes, and snippets.

@formula1
Created March 2, 2017 21:24
Show Gist options
  • Save formula1/7ac36e4bbfc7bacd0364d0c7ceca0e0d to your computer and use it in GitHub Desktop.
Save formula1/7ac36e4bbfc7bacd0364d0c7ceca0e0d to your computer and use it in GitHub Desktop.
Simple React Server
module.exports = function(props){
return (
<html>
<head></head>
<body>
<h1>This is an example</h1>
<ul>{props.data.map(function(item){
return <li>{item.toString()}</li>;
})}</ul>
</body>
</html>
);
};
{
"name": "serverside-react-example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-core": "^6.23.1",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.23.0",
"express": "^4.15.0",
"react": "^15.4.2",
"react-dom": "^15.4.2"
}
}
require('babel-core/register')({
presets: ['es2015', 'react']
});
var express = require("express");
var ReactDomServer = require("react-dom/server");
var React = require("react");
var server = express();
server.get("/", function(req, res){
res.send("hello world");
});
server.get("/example", function(req, res){
var View = require("./example");
var props = {
data: [1, 2, 3]
};
res.send(
"<!doctypehtml>\n" +
ReactDomServer.renderToStaticMarkup(
React.createElement(View, props, null)
)
);
});
server.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment