Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion bin/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const http = require('http');
const httpProxy = require('http-proxy');
const url = require('url');
const config = require('../config');
const server = require('../server/main');
const _debug = require('debug');
Expand All @@ -6,6 +9,18 @@ const debug = _debug('app:bin:server');
const port = config.server_port;
const host = config.server_host;

server.listen(port, host, () => {
const proxy = config.proxy && config.proxy.enabled ? httpProxy.createProxyServer() : null;
const serverInstance = http.createServer((req, res) => {
if (proxy && config.proxy.match.test(url.parse(req.url).pathname)) {
return proxy.web(req, res, config.proxy.options);
}
server.callback()(req, res);
}).listen(port, host, () => {
debug(`Server is now running at http://${host}:${port}.`);
});

if (proxy) {
serverInstance.on('upgrade', (req, socket, head) => {
proxy.ws(req, socket, head, config.proxy.options_ws);
});
}
8 changes: 6 additions & 2 deletions config/environments.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ module.exports = {
compiler_devtool: 'cheap-module-eval-source-map',
proxy: {
enabled: true,
match: /^\/(api|socket\.io)\/.*/,
options: {
host: 'http://localhost:8000',
match: /^\/api\/.*/
target: 'http://localhost:8000'
},
options_ws: {
target: 'ws://localhost:8000',
ws: true
}
}
}),
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"better-npm-run": "0.0.10",
"debug": "^2.2.0",
"fs-extra": "^0.30.0",
"http-proxy": "^1.14.0",
"koa": "^2.0.0-alpha.3",
"koa-compress": "^2.0.0",
"koa-connect-history-api-fallback": "^0.3.0",
Expand Down Expand Up @@ -176,6 +177,7 @@
"rimraf": "^2.5.4",
"sinon": "^1.17.5",
"sinon-chai": "^2.8.0",
"socket.io-client": "^1.4.8",
"sort-by": "^1.1.1",
"style-loader": "^0.13.0",
"svg-inline-loader": "^0.6.1",
Expand Down
7 changes: 0 additions & 7 deletions server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ const debug = require('debug')('app:server');
const paths = config.utils_paths;
const app = new Koa();

// Enable koa-proxy if it has been enabled in the config.
if (config.proxy && config.proxy.enabled) {
debug('enabling proxy', config.proxy.options);
const proxy = require('koa-proxy');
app.use(convert(proxy(config.proxy.options)));
}

// use gzip
app.use(compress());

Expand Down
3 changes: 3 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createHistory } from 'history';
import { useRouterHistory } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux';
import injectTapEventPlugin from 'react-tap-event-plugin';
import io from 'socket.io-client';
import createStore from './store/createStore';
import Application from './containers/Application';

Expand All @@ -27,6 +28,8 @@ const browserHistory = useRouterHistory(createHistory)({
// so we need to provide a custom `selectLocationState` to inform
// react-router-redux of its location.
const initialState = window.___INITIAL_STATE__;
const socket = io();
socket.on('news', data => console.log('Backend msg', data));
const store = createStore(initialState, browserHistory);
const history = syncHistoryWithStore(browserHistory, store, {
selectLocationState: (state) => state.router
Expand Down