我试图使cors-anywhere工作在我的nodejs/express没有成功。
首先我像这样使用express-cors-anywhere
:
const anywhere = require('express-cors-anywhere')
app.use("/cors-anywhere", anywhere())
但是得到以下错误:
anywhere is not a function
我已经尝试使用本地cors-anywhere
库,但我不知道如何实现这在一个快速的js应用程序:
var cors_proxy = require('cors-anywhere');
cors_proxy.createServer({
originWhitelist: [], // Allow all origins
requireHeader: ['origin', 'x-requested-with'],
removeHeaders: ['cookie', 'cookie2']
}).listen(port, host, function() {
console.log('Running CORS Anywhere on ' + host + ':' + port);
});
有什么建议吗?
如何与现有expressjs服务器集成#81
let proxy = corsAnywhere.createServer({
originWhitelist: [], // Allow all origins
requireHeaders: [], // Do not require any headers.
removeHeaders: [] // Do not remove any headers.
});
/* Attach our cors proxy to the existing API on the /proxy endpoint. */
api.get('/proxy/:proxyUrl*', (req, res) => {
req.url = req.url.replace('/proxy/', '/'); // Strip '/proxy' from the front of the URL, else the proxy won't work.
proxy.emit('request', req, res);
});