// https://github.com/jlongster/react-redux-universal-hot-example/blob/master/src/server.js
const http =require('http')
const Url = require('url')
const React = require('react')
const { renderToString } = require('react-dom/server')
const { Provider } = require('react-redux')
const { createHistory } = require('history')
const { Router, RoutingContext, match } = require('react-router')
const sendHtml = require('send-data/html')
const sendError = require('send-data/error')
const redirect = require('predirect')
const createStore = require('app/store')
const routes = require('app/routes')
const fetchAllData = require('app/util/fetch-all-data')
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 =