其實還蠻容易的…
首先另外開一個nodejs project來安裝 CORS-Anywhere package
npm install cors-anywhere
然後編輯 server.js
// Heroku defines the environment variable PORT, and requires the binding address to be 0.0.0.0
var host = process.env.PORT ? '0.0.0.0' : '127.0.0.1';
var port = process.env.PORT || 5566;
var cors_proxy = require('cors-anywhere');
cors_proxy.createServer({
originWhitelist: [], // Allow all origins
// comment 下面這行就可以接受來自其他 domain 的 request
// requireHeader: ['Origin', 'X-Requested-With'],
removeHeaders: ['cookie', 'cookie2']
}).listen(port, host, function() {
console.log('Running CORS Anywhere on ' + host + ':' + port);
});
接著就可以執行這個 cors-anywhere proxy service
node server.js
接著就是需要做 CORS request 的頁面,可以用 JavaScript xhttp request 來送 request 給 cors-anywhere proxy了。