Http-proxy-middleware: Cannot proxy requests to Heroku

Created on 3 Mar 2017  路  2Comments  路  Source: chimurai/http-proxy-middleware

Expected behavior

http-proxy-middleware should proxy my requests from my localhost to my remote Heroku API, successfully returning Heroku's response.

Actual behavior

The request just hangs. No error is thrown. Eventually the client gives up and reports 499 Client Disconnected.

Setup

  • http-proxy-middleware: ^0.17.1
  • server: express ^4.14.0
  • I'm using a few other things (compression, body-parser) but I don't think they're getting in my way.

proxy middleware configuration

const fs          = require('fs'),
      express     = require('express'),
      bodyParser  = require('body-parser'),
      app         = express(),
      compress    = require('compression'),
      packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8')),
      Routes      = require('./Routes'),
      httpProxy = require('http-proxy-middleware');

// NOTE: not my real subdomain
const API_SERVER = 'https://my-api.herokuapp.com';

app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({extended: false}));

app.disable('x-powered-by');

// Enable HTTP gzip compression.
app.use(compress());

// apply reverse proxy to heroku API
app.use('/api', httpProxy({
    logLevel     : 'debug',
    target       : API_SERVER,
    changeOrigin : true,
    secure       : false,
    xfwd         : true,
    router: {
        '/api/auth/login': `${API_SERVER}/auth/login`
    },
    onProxyReq   : function (proxyReq, req, res) {
        // Browers may send Origin headers even with same-origin
        // requests. To prevent CORS issues, we have to change
        // the Origin to match the target URL.
        if (proxyReq.getHeader('origin')) {
            proxyReq.setHeader('origin', API_SERVER);
        }
    }
}));

app.all('*', function(req, res, next) {
    res.set({
        'www-version': `${packageJson.version}`,
        'X-Frame-Options': 'DENY',
        'Cache-control': 'no-store',
        'Pragma': 'no-cache',
        'Strict-Transport-Security': 'max-age=' + (365 * 24 * 60 * 60) // 365 days, in seconds
    });
    next();
});

// apply other, non-proxied routes
app.use(Routes);

// Define the port.
const port = process.env.PORT || 4000;

// Start the HTTP server.
app.listen(port, () => {
    console.log(`App listening on port ${port}!`);
});
question

Most helpful comment

Think the body-parser _does_ get in the way.

Try changing the order of the middleware by configuring the httpProxy before the bodyParser.
(https://github.com/chimurai/http-proxy-middleware/issues/40#issuecomment-163398924)

or

Restream parsed body: https://github.com/chimurai/http-proxy-middleware/issues/40#issuecomment-249430255

All 2 comments

Think the body-parser _does_ get in the way.

Try changing the order of the middleware by configuring the httpProxy before the bodyParser.
(https://github.com/chimurai/http-proxy-middleware/issues/40#issuecomment-163398924)

or

Restream parsed body: https://github.com/chimurai/http-proxy-middleware/issues/40#issuecomment-249430255

OMFG I have been trying to make this work for 2 days and that totally did it. Thanks so much!!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mshindal picture mshindal  路  4Comments

noahgrant picture noahgrant  路  4Comments

pwlerke picture pwlerke  路  6Comments

georgyfarniev picture georgyfarniev  路  7Comments

nikksan picture nikksan  路  3Comments