【发布时间】:2016-04-13 13:18:37
【问题描述】:
我需要在第三方服务上解决CORS,所以我想建立一个代理来添加标题“Access-Control-Allow-Origin: *”。
为什么这段代码没有添加标题?
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);
【问题讨论】:
-
尝试使用模块cors,
npm install cors