forked from react-navigation/react-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.js
More file actions
94 lines (86 loc) · 2.71 KB
/
Copy pathServer.js
File metadata and controls
94 lines (86 loc) · 2.71 KB
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
const path = require('path');
const express = require('express');
const fs = require('fs');
const React = require('react');
const PropTypes = require('prop-types');
const join = require('path').join;
const basicAuth = require('basic-auth-connect');
const server = require('express');
const ReactDOMServer = require('react-dom/server');
import App from './App';
import { NavigationActions, addNavigationHelpers } from 'react-navigation';
class ServerApp extends React.Component {
static childContextTypes = {
getURIForAction: PropTypes.func.isRequired,
getActionForPathAndParams: PropTypes.func.isRequired,
dispatch: PropTypes.func.isRequired,
};
getChildContext() {
return {
dispatch: this.props.navigation.dispatch,
getURIForAction: action => {
const state = App.router.getStateForAction(action);
let { path } = App.router.getPathAndParamsForState(state);
return `/${path}`;
},
getActionForPathAndParams: App.router.getActionForPathAndParams,
};
}
render() {
return <App navigation={this.props.navigation} />;
}
}
const indexHtml = fs.readFileSync(join(__dirname, '../public/index.html'), {
encoding: 'utf8',
});
function AppHandler(req, res) {
let status = 200;
const path = req.url.substr(1);
let initAction = App.router.getActionForPathAndParams(path);
if (!initAction) {
initAction = NavigationActions.navigate({
routeName: 'NotFound',
params: { path },
});
status = 404;
}
const topNavigation = addNavigationHelpers({
state: App.router.getStateForAction(initAction),
dispatch: action => false,
});
const screenNavigation = addNavigationHelpers({
state: topNavigation.state.routes[topNavigation.state.index],
dispatch: topNavigation.dispatch,
});
const Component = App.router.getComponentForState(topNavigation.state);
const { title } = App.router.getScreenOptions(screenNavigation, {});
const app = <ServerApp navigation={topNavigation} />;
const body = ReactDOMServer.renderToString(app);
let html = indexHtml;
html = html
.split('<div id="root"></div>')
.join(`<div id="root">${body}</div>`);
if (title) {
html = html.split('<title></title>').join(`<title>${title}</title>`);
}
res.status(status).send(html);
}
const app = express();
app.use(function(req, res, next) {
var str = 'www.';
if (req.host.indexOf(str) === 0) {
res.redirect(
301,
req.protocol + '://' + req.host.slice(str.length) + req.originalUrl
);
} else {
next();
}
});
app.get('/', AppHandler);
app.use(express.static(join(__dirname, '../public')));
app.get('*', AppHandler);
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Started on ${PORT}!`);
});