I'm using expressjs to create and API. I also have a proxy server to direct requests to my API Server, as follows:
But on the API server the body of POST and PUT requests are unable to parse and the server hungs.
// on proxy server
const httpProxy = require('http-proxy');
const proxy = httpProxy.createProxyServer({});
app.all('/api/*', function(req, res){
proxy.web(req, res, { target: 'http://example.com:9000' });
});
proxy.on('proxyReq', function(proxyReq, req, res, options) {
if ((req.method == "POST" || req.method == "PUT") && req.body) {
proxyReq.write(JSON.stringify(req.body));
proxyReq.end();
}
});
proxy.on('proxyRes', function(proxyRes, req, res) {
var body = '';
proxyRes.on('data', function(chunk) {
body += chunk;
});
proxyRes.on('end', function() {
});
});
Part of the api server to parse body content.
// on api server
app.use(bodyParser.json({ }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(compress());
app.use(methodOverride());
I realized it hungs in the bodyParser()
middleware.
Any help will be greatly appreciated. I tried various solution already on stackoverflow and none has worked for me.