Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot proxy requests to Heroku #150

Closed
arthurakay opened this issue Mar 3, 2017 · 2 comments
Closed

Cannot proxy requests to Heroku #150

arthurakay opened this issue Mar 3, 2017 · 2 comments
Labels

Comments

@arthurakay
Copy link

arthurakay commented Mar 3, 2017

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}!`);
});
@chimurai
Copy link
Owner

chimurai commented Mar 3, 2017

Think the body-parser does get in the way.

Try changing the order of the middleware by configuring the httpProxy before the bodyParser.
(#40 (comment))

or

Restream parsed body: #40 (comment)

@arthurakay
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants