Created
March 2, 2017 21:24
-
-
Save formula1/7ac36e4bbfc7bacd0364d0c7ceca0e0d to your computer and use it in GitHub Desktop.
Simple React Server
This file contains 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
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> | |
); | |
}; |
This file contains 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
{ | |
"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" | |
} | |
} |
This file contains 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
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