-
Notifications
You must be signed in to change notification settings - Fork 1
/
routing.js
30 lines (27 loc) · 924 Bytes
/
routing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Created by mikhail on 16.07.15.
*/
import React from 'react';
import Router from 'react-router';
import Promise from 'promise';
import routes from '../../client/routing/routes.js';
import renderFullPage from '../../utils/renderFullPage.js';
import router from '../../client/routing/router.js';
import createLocation from 'history/lib/createLocation';
import createMemoryHistory from 'history/lib/createMemoryHistory';
const history = createMemoryHistory();
export default function routing(req, res) {
let location = createLocation(req.path);
router(location, history, req, res)
.then((reactEl) => {
try {
let reactStr = React.renderToString(reactEl);
res.send(renderFullPage(reactStr));
} catch (err) {
res.status(500).send({error: err.toString()});
}
}, ({message, code}) => {
console.error(message, code);
res.sendStatus(code || 500);
});
}