-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestHandler.js
137 lines (117 loc) · 5.1 KB
/
requestHandler.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// "use strict"
// import axios from 'axios';
// // var axios = require('axios')
// import React from 'react';
// // var React = require('react')
// import {createStore} from 'redux';
// // var createStore = require('redux').createStore;
// import {Provider} from 'react-router';
// // var Provider = require('react-router').Provider;
// import{renderToString} from 'react-dom/server';
// // var renderToString = require('react-dom/server').renderToString
// // import {match, RouterContext} from 'react-router';
// import {StaticRouter} from 'react-router-dom';
// // var match = require('react-router').match
// // var RouterContext = require('react-router').RouterContext
// import reducers from './src/reducers/index'
// // var reducers = require('./src/reducers/index').combineReducers
// import routes from './src/routes'
// // var routes = require('./src/routes').routes
// function handleRender(req, res){
// axios.get('http://localhost:3001/books').then(function(response){
// // var myHtml = JSON.stringify(response.data);
// // res.render('index', {myHtml});
// // STEP-1 CREATE A REDUX STORE ON THE SERVER
// const store = createStore(reducers, {'books':{'books':response.data}})
// // STEP-2 GET INITIAL STATE FROM THE STORE
// const initialState = JSON.stringify(store.getState()).replace(/<\/script/g, '<\\/script').replace(/<!--/g, '<\\!--');
// // STEP-3 IMPLEMENT REACT-ROUTER ON THE SERVER TO INTERCEPT CLIENT REQUESTs AND DEFINE WHAT TO DO WITH THEM
// const context = {};
// console.log("How context looks like? ", context.url);
// const reactComponent = renderToString(
// <Provider store={store}>
// <StaticRouter
// location={req.url}
// context={context}>
// {routes}
// </StaticRouter>
// </Provider>
// );
// if (context.url) {
// // can use the `context.status` that
// // we added in RedirectWithStatus
// redirect(context.status, context.url)
// } else {
// res.status(200).render('index', {reactComponent, initialState})
// }
// // const Routes = {
// // routes:routes,
// // location: req.url
// // }
// // match(Routes, function(error, redirect, props){
// // if(error){
// // res.status(500).send("Error fullfillinf the request");
// // } else if(redirect){
// // res.status(301, redirect.pathname + redirect.search)
// // } else if(props){
// // const reactComponent = renderToString(
// // <Provider store={store}>
// // <RouterContext {...props}/>
// // </Provider>
// // )
// // res.status(200).render('index', {reactComponent, initialState})
// // } else {
// // res.status(404).send('Not Found')
// // }
// // })
// }).catch(function(err){
// console.log('#Initial Server-side rendering error', err);
// })
// }
// module.exports = handleRender;
"use strict"
import axios from 'axios';
import React from 'react';
import {createStore} from 'redux';
import {Provider} from 'react-redux';
import {renderToString} from 'react-dom/server';
// REACT-ROUTER 4 CHANGES
//import {match, RouterContext} from 'react-router';
import {StaticRouter} from 'react-router-dom';
//import { renderToString } from 'react-router-server';
import reducers from './src/reducers/index';
import routes from './src/routes';
function handleRender(req, res){
axios.get('http://localhost:3001/books')
.then(function(response){
// var myHtml = JSON.stringify(response.data);
// res.render('index', {myHtml});
// STEP-1 CREATE A REDUX STORE ON THE SERVER
const store = createStore(reducers, {"books":{"books":response.data}})
// STEP-2 GET INITIAL STATE FROM THE STORE
const initialState = JSON.stringify(store.getState()).replace(/<\/script/g, '<\\/script').replace(/<!--/g, '<\\!--');
// STEP-3 IMPLEMENT REACT-ROUTER ON THE SERVER TO INTERCEPT CLIENT REQUESTs AND DEFINE WHAT TO DO WITH THEM
const context = {};
console.log("How context looks like? ", context.url);
const reactComponent = renderToString(
<Provider store={store}>
<StaticRouter
location={req.url}
context={context}>
{routes}
</StaticRouter>
</Provider>
);
if (context.url) {
// can use the `context.status` that
// we added in RedirectWithStatus
redirect(context.status, context.url)
} else {
res.status(200).render('index', {reactComponent, initialState})
}
})
.catch(function(err){
console.log('#Initial Server-side rendering error', err);
})
}
module.exports = handleRender