Last active
March 28, 2017 16:02
-
-
Save namelos/310863330dda84cc478fb678ace3f626 to your computer and use it in GitHub Desktop.
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
const React = require('react') | |
const ReactDOM = require('react-dom/server') | |
const express = require('express') | |
const app = express() | |
const html = content => ` | |
<!doctype html> | |
<html> | |
<body> | |
${content} | |
</body> | |
</html> | |
` | |
const Hello = ({ name }) => React.createElement('h1', {}, `Hello ${name}`) | |
const renderView = (conn, component, props) => | |
conn.res.send(html(ReactDOM.renderToString(component))) | |
const renderJSON = (conn, obj) => | |
conn.res.send(JSON.stringify(obj)) | |
const get = | |
(app => | |
(route, handler) => | |
app.get(route, (req, res) => | |
handler({req, res})))(app) | |
// -- api | |
get('/', conn => renderView(conn, React.createElement(Hello, { name: 'namelos' }))) | |
get('/json', conn => renderJSON(conn, { name: 'namelos' })) | |
app.listen(3000, () => console.log('listening')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment