// https://github.com/jlongster/react-redux-universal-hot-example/blob/master/src/server.js
import http from 'http'
import Url from 'url'
import React from 'react'
import { renderToString } from 'react-dom/server'
import { Provider } from 'react-redux'
import { createHistory } from 'history'
import { Router, RoutingContext, match } from 'react-router'
import sendHtml from 'send-data/html'
import sendError from 'send-data/error'
import redirect from 'predirect'
import createStore from 'app/store'
import routes from 'app/routes'
import fetchAllData from 'app/util/fetch-all-data'
export default module.exports = createRender
function createRender (config) {
const staticUrl = Url.format(config.static.url)
return http.createServer(render)
function render (req, res) {
const store = createStore()
match({
routes: routes,
location: req.url
}, function (err, redirectLocation, renderProps) {
if (redirectLocation) {
redirect(req, res, redirectLocation.pathname + redirectLocation.search)
} else if (err) {
sendError(req, res, { body: err })
} else if (!renderProps) {
sendError(req, res, {
statusCode: 404,
body: new Error('Not found')
})
} else {
fetchAllData(
renderProps.components,
store.getState, store.dispatch,
renderProps.location,
renderProps.params
).then(function () {
const component =