-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.jsx
94 lines (75 loc) · 2.27 KB
/
server.jsx
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
import express from 'express';
import React from 'react';
import { renderToString } from 'react-dom/server'
import { RouterContext, match } from 'react-router';
import { Router, browserHistory } from 'react-router';
// import createLocation from 'history';
import routes from 'routes';
import path from 'path';
const app = express();
if (process.env.NODE_ENV !== 'production') {
require('./webpack.dev').default(app);
}
app.use(express.static(path.join(__dirname, 'dist')));
app.use( (req, res) => {
// const location = location: req.url
match({ routes, location: req.url }, (err, redirectLocation, renderProps) => {
if(err) {
console.error(err);
return res.status(500).end('Internal server error');
}
if(!renderProps)
return res.status(404).end('Not found');
function renderView() {
const InitialView = (
<RouterContext {...renderProps} />
);
const componentHTML = renderToString(InitialView);
const HTML = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Redux Demo</title>
</head>
<body>
<div id="react-view">${componentHTML}</div>
<script type="application/javascript" src="/bundle.js"></script>
</body>
</html>
`;
return HTML;
}
const html = renderView();
res.end(html);
});
});
//import App from './shared/components/index';
// app.use(express.static(path.join(__dirname, 'dist')));
// app.get('/', function(request, response){
// var html = renderToString(
// React.createElement(App)
// );
// //response.send(App);
// response.end(html);
// });
//
//
// app.use(function(req, res, next) {
//
//
// match({ routes, location: req.url }, (error, redirectLocation, renderProps) => {
// if (error) {
// res.status(500).send(error.message)
// } else if (redirectLocation) {
// res.redirect(302, redirectLocation.pathname + redirectLocation.search)
// } else if (renderProps) {
// res.status(200).send(renderToString(<RoutingContext {...renderProps} />))
// } else {
// res.status(404).send('Not found')
// }
// })
//
//
// })
export default app;