Created
February 4, 2017 03:31
-
-
Save divmain/409461325d8098dfab08ff46b38cbef8 to your computer and use it in GitHub Desktop.
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
import React, { Component } from "react"; | |
import express from "express"; | |
import path from "path"; | |
import { renderToStream, toNodeStream, streamTemplate } from "rapscallion"; | |
import { Route, StaticRouter } from "react-router-dom"; | |
class App extends Component { | |
render() { | |
return ( | |
<Route path="/" component={Home} /> | |
) | |
} | |
} | |
class Home extends Component { | |
constructor(props) { | |
super(props) | |
} | |
render() { | |
return ( | |
<div className="home">Test</div> | |
) | |
} | |
} | |
const app = express(); | |
app.use("*", (req, res) => { | |
const context = {}; | |
const component = ( | |
<StaticRouter location={req.url} context={context}> | |
<App /> | |
</StaticRouter> | |
); | |
const application = renderToStream(component); | |
const html = streamTemplate`<!doctype html><html> | |
<head> | |
<title>Test</title> | |
</head> | |
<body> | |
<div id="root">${application}</div> | |
</body> | |
</html>` | |
if (context.url) { | |
res.writeHead(302, { Location: context.url }); | |
res.end(); | |
} else { | |
toNodeStream(html).pipe(res) | |
} | |
}); | |
app.listen(3000, () => { | |
console.log("App listening on port 3000"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example comment