Last active
February 16, 2017 11:37
-
-
Save pahund/f07b12859d3dacf87af81a7bb07a341e to your computer and use it in GitHub Desktop.
Code exerpts from universal-react-router4 – server/index.js
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
const routes = [ | |
'/', | |
'/g/:gistId' | |
]; | |
app.get('*', (req, res) => { | |
const match = routes.reduce((acc, route) => matchPath(req.url, route, { exact: true }) || acc, null); | |
if (!match) { | |
res.status(404).send(render(<NoMatch />)); | |
return; | |
} | |
fetch('https://api.github.com/gists') | |
.then(r => r.json()) | |
.then(gists => | |
res.status(200).send(render( | |
( | |
<StaticRouter context={{}} location={req.url}> | |
<App gists={gists} /> | |
</StaticRouter> | |
), gists | |
)) | |
).catch(err => res.status(500).send(render(<Error />)); | |
}); | |
app.listen(3000, () => console.log('Demo app listening on port 3000')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment