diff --git a/bin/server.js b/bin/server.js index 16e0174..46ffe08 100644 --- a/bin/server.js +++ b/bin/server.js @@ -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'); @@ -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); + }); +} diff --git a/config/environments.js b/config/environments.js index 004db25..3b4e6f6 100644 --- a/config/environments.js +++ b/config/environments.js @@ -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 } } }), diff --git a/package.json b/package.json index ed5320c..a3c87e2 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/server/main.js b/server/main.js index f1f6cd9..d6e7e76 100644 --- a/server/main.js +++ b/server/main.js @@ -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()); diff --git a/src/main.js b/src/main.js index c22ada4..3a657e4 100644 --- a/src/main.js +++ b/src/main.js @@ -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'; @@ -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