Skip to content

Instantly share code, notes, and snippets.

@namelos
Last active March 28, 2017 16:02
Show Gist options
  • Save namelos/310863330dda84cc478fb678ace3f626 to your computer and use it in GitHub Desktop.
Save namelos/310863330dda84cc478fb678ace3f626 to your computer and use it in GitHub Desktop.
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