'use strict';

function ProxyServerAddon(project) {
  this.project = project;
  this.name = 'proxy-server-middleware';
}

ProxyServerAddon.prototype.serverMiddleware = function(options) {
  var app = options.app, server = options.options.httpServer;
  options = options.options;

  if (options.proxy) {
    var proxy = require('http-proxy').createProxyServer({
      target: options.proxy,
      ws: true,
      secure: !options.insecureProxy,
      changeOrigin: true
    });

    proxy.on('error', function (e) {
      options.ui.writeLine('Error proxying to ' + options.proxy);
      options.ui.writeError(e);
    });

    var morgan = require('morgan');

    options.ui.writeLine('Proxying to ' + options.proxy);

    server.on('upgrade', function (req, socket, head) {
      options.ui.writeLine('Proxying websocket to ' + req.url);
      proxy.ws(req, socket, head);
    });

    app.use(morgan('dev'));
    app.use(function(req, res) {
      return proxy.web(req, res);
    });
  }
};

module.exports = ProxyServerAddon;

'use strict';

function ProxyServerAddon(project) {
  this.project = project;
  this.name = 'proxy-server-middleware';
}

ProxyServerAddon.prototype.serverMiddleware = function(options) {
  var app = options.app, server = options.options.httpServer;
  options = options.options;

  if (options.proxy) {
    var proxy = require('http-proxy').createProxyServer({
      target: options.proxy,
      ws: true,
      secure: !options.insecureProxy,
      changeOrigin: true,
      xfwd: true
    });

    proxy.on('error', function (e) {
      options.ui.writeLine('Error proxying to ' + options.proxy);
      options.ui.writeError(e);
    });

    var morgan = require('morgan');

    options.ui.writeLine('Proxying to ' + options.proxy);

    server.on('upgrade', function (req, socket, head) {
      options.ui.writeLine('Proxying websocket to ' + req.url);
      proxy.ws(req, socket, head);
    });

    app.use(morgan('dev'));
    app.use(function(req, res) {
      return proxy.web(req, res);
    });
  }
};

module.exports = ProxyServerAddon;

Enable X-Forwarded-* headers in http-proxy
Enable X-Forwarded-* headers in http-proxy

Reasoning: when using e.g. a Rails backend running on a separate port, proxying it using the ember-cli proxy feature, the generated _links_ sometimes tends to be incorrect. They are based on settings in the X-Forwarded-* headers. Hence, xfwd must be enabled.

If you like, we can make this a command-line option.

/cc @perlun, @jesjos