I have a proxy server which modifies a specific type of POST Request before forwarding it to the Server.
The Modification requires me to make a request to another server in order to get the new value that will be put in place of the old one in the POST Request.
I can't get http-proxy-middleware
's onProxyReq
function to block until I get the new value.
Anything will help. Thanks.
Code:
const options = {
target: 'http://localhost:8998',
changeOrigin: true,
onProxyReq: (proxyReq, req, res) => {
if (req.path === '/batches' && req.body && req.body.file) {
request.post('MAKES REQUEST TO ANOTHER SERVER FOR NEW VALUE',{json: {}},(error,res,body)=>{
req.body.url=body.url;
});
const bodyData = JSON.stringify(req.body);
proxyReq.setHeader('Content-Type', 'application/json');
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
proxyReq.write(bodyData);
}else{
const bodyData = JSON.stringify(req.body);
proxyReq.setHeader('Content-Type', 'application/json');
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
proxyReq.write(bodyData);
}
},
};
app.use(createProxyMiddleware(options));
The problem is that the POST request is forwarded before the new value can be received from the request.post
operation.