Created
October 12, 2015 18:23
-
-
Save philippTheCat/a8204330458832a83376 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
"use strict"; | |
import express from "express"; | |
var routes = require("./common"); | |
var app = express() | |
import { renderToString } from 'react-dom/server' | |
import { match, RoutingContext } from 'react-router' | |
var React = require("react"); | |
var StateLoader = require("./lib/StateLoader"); | |
import { createMemoryHistory } from 'history' | |
const history = createMemoryHistory(); | |
app.use('/', function(req, res){ | |
const location = history.createLocation(req.url); | |
match({ routes, location: location }, (error, redirectLocation, renderProps) => { | |
if (error) { | |
res.status(500).send(error.message) | |
} else if (redirectLocation) { | |
res.redirect(302, redirectLocation.pathname + redirectLocation.search) | |
} else if (renderProps) { | |
StateLoader.loadPrerenderStateFromServer(); | |
res.status(200).send(renderToString(<RoutingContext {...renderProps} />)) | |
} else { | |
res.send(404, 'Not found') | |
} | |
}) | |
}); | |
app.listen(3333) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment