http-proxy-middleware should proxy my requests from my localhost to my remote Heroku API, successfully returning Heroku's response.
The request just hangs. No error is thrown. Eventually the client gives up and reports 499 Client Disconnected.
compression, body-parser) but I don't think they're getting in my way.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}!`);
});
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!!!
Most helpful comment
Think the
body-parser_does_ get in the way.Try changing the order of the middleware by configuring the
httpProxybefore thebodyParser.(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