Closed
Description
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}!`);
});
Activity
chimurai commentedon Mar 3, 2017
Think the
body-parser
does get in the way.Try changing the order of the middleware by configuring the
httpProxy
before thebodyParser
.(#40 (comment))
or
Restream parsed body: #40 (comment)
arthurakay commentedon Mar 3, 2017
OMFG I have been trying to make this work for 2 days and that totally did it. Thanks so much!!!