你有proxyRes
活动.
所以这样的事情应该有效:
proxy.on('proxyRes', function(proxyRes, req, res) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS'); });
完整的工作示例(好吧,当我说完整时,我并不是说这是一个安全 - 故障安全 - 真正的代理,但它可以解决您的问题):
var http = require('http'), httpProxy = require('http-proxy'); var proxy = httpProxy.createProxyServer({}); var server = http.createServer(function(req, res) { proxy.web(req, res, { target: 'https://third_party_server...', secure: false, ws: false, prependPath: false, ignorePath: false, }); }); console.log("listening on port 8000") server.listen(8000); // Listen for the `error` event on `proxy`. // as we will generate a big bunch of errors proxy.on('error', function (err, req, res) { console.log(err) res.writeHead(500, { 'Content-Type': 'text/plain' }); res.end("Oops"); }); proxy.on('proxyRes', function(proxyRes, req, res) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS'); });