import React from 'react' import { renderToString } from 'react-dom/server' import { Provider } from 'react-redux' import { match, RouterContext } from 'react-router' import routes from '../common/routes.js' export default function render(store, req, res) { // Send the rendered page back to the client match({ routes, location: req.url }, (error, redirectLocation, renderProps) => { if (error) { console.log(error) res.status(500).send(renderError('Routing Error:', error.message)) } else if (redirectLocation) { res.redirect(302, redirectLocation.pathname + redirectLocation.search) } else if (renderProps) { // Render the component to a string try { const html = renderToString( ) // Grab the initial state from our Redux store const initialState = store.getState() // and send res.status(200).send(appTemplate(html, initialState)) } catch(ex) { console.log("Render Exception:",ex) res.status(500).send(errorTemplate('Render Exception', ex.message, ex)) } } else { res.status(404).send(errorTemplate('Not found', `${req.url} not found.`)) } }) } const pageTemplate = (body) => { return ` Dubdiff ${body} ` } function errorTemplate(title, message, exception) { return pageTemplate(`

${title}

${message}

${exception ? `
${exception.toString()}
`: `` } `) } function appTemplate(html, initialState) { return pageTemplate(`
${html}
`) }