-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
94 lines (78 loc) · 2.72 KB
/
app.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
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import path from 'path';
import compression from 'compression';
import { matchRoutes } from 'react-router-config';
import { createStore, applyMiddleware } from 'redux';
import reduxThunk from 'redux-thunk';
import proxy from 'express-http-proxy';
import axios from 'axios';
import RouteApp from './src/front/route';
import renderApp from './src/back/service/render';
import reducers from './src/front/reducers';
// const passport = require('passport');
// const cookieSession = require('cookie-session');
// const bluebird = require('bluebird');
// const mongoose = require('mongoose');
// const database = require('./src/back/config/database');
// const key = require('./src/back/config/key');
// mongoose.Promise = bluebird;
// mongoose.connect(database.url, { useNewUrlParser: true });
// mongoose.connection.on('connected', () => {
// console.log('Connected to database');
// });
// mongoose.connection.on('error', (error) => {
// console.log(error);
// });
const app = express();
const port = 3000;
app.use(compression({ level: 9 }));
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// app.use(cookieSession({
// maxAge: 24 * 60 * 60 * 1000 * 12,
// keys: [key.cookie],
// }));
// app.use(passport.initialize());
// app.use(passport.session());
// require('./src/back/service/auth')(passport);
// const api = require('./src/back/route');
// app.use('/api', api);
app.use('/api',
proxy('http://react-ssr-api.herokuapp.com', {
// Handle potential security errors with google oauth flow to API server
proxyReqOptDecorator(opts) {
opts.headers['x-forwarded-host'] = 'localhost:3000';
return opts;
},
}));
app.use('/build', express.static(path.join(__dirname, 'build')));
app.get('*', (req, res) => {
const axiosInstance = axios.create({
baseURL: 'http://react-ssr-api.herokuapp.com/',
headers: { Cookie: req.cookies || '' },
});
const store = createStore(reducers, applyMiddleware(reduxThunk.withExtraArgument(axiosInstance)));
const promise = matchRoutes(RouteApp, req.path).map(({ route }) => (route.loadData ? route.loadData(store) : null));
let preloadedState;
const context = {};
const content = () => renderApp(req, store, preloadedState, context);
Promise.all(promise).then(() => {
preloadedState = store.getState();
if (context.notFound) {
res.status(404);
}
if (context.url) {
res.redirect(301, context.url);
}
res.send(content());
}).catch((err) => {
preloadedState = store.getState();
res.status(err.response.status).send(content());
});
});
app.listen(port, () => {
console.log('Server Started');
});