Skip to content

Instantly share code, notes, and snippets.

@divmain
Created February 4, 2017 03:31
Show Gist options
  • Select an option

  • Save divmain/409461325d8098dfab08ff46b38cbef8 to your computer and use it in GitHub Desktop.

Select an option

Save divmain/409461325d8098dfab08ff46b38cbef8 to your computer and use it in GitHub Desktop.
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");
});
@divmain
Copy link
Copy Markdown
Author

divmain commented Apr 12, 2019

example comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment