I need to solve CORS on a third party service, so I want to build a proxy to add the header "Access-Control-Allow-Origin: *".
Why is this code is not adding the header?
httpProxy = require('http-proxy');
var URL = 'https://third_party_server...';
httpProxy.createServer({ secure: false, target: URL }, function (req, res, proxy) {
res.oldWriteHead = res.writeHead;
res.writeHead = function(statusCode, headers) {
/* add logic to change headers here */
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
res.oldWriteHead(statusCode, headers);
}
proxy.proxyRequest(req, res, { secure: false, target: URL });
}).listen(8000);
You have the
proxyRes
event available.So something like that should work:
Full working example (well, when I say full I do not mean this is a secure-failsafe-real proxy, but it does the job for your question):