Skip to content

Instantly share code, notes, and snippets.

@philippTheCat
Created October 12, 2015 18:23
Show Gist options
  • Save philippTheCat/a8204330458832a83376 to your computer and use it in GitHub Desktop.
Save philippTheCat/a8204330458832a83376 to your computer and use it in GitHub Desktop.
"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