CORS 在任何地方返回代理文本,而不是所需的目标资源
CORS anywhere returning proxy text, not desired target resource
我正在尝试使用 node、express 和 cors-anywhere 实例为我的 arcgis-js-api 应用设置代理。我的服务器文件如下所示:
import express from 'express';
import cors from 'cors';
import corsAnywhere from 'cors-anywhere';
const { PORT } = process.env;
const port = PORT || 3030;
var app = express();
let proxy = corsAnywhere.createServer({
originWhitelist: [], // Allow all origins
requireHeaders: [], // Do not require any headers.
removeHeaders: [], // Do not remove any headers.
});
app.use(cors());
app.get('/proxy/:proxyUrl*', (req, res) => {
req.url = req.url.replace('/proxy/', '/');
proxy.emit('request', req, res);
});
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'index.html'));
});
app.listen(port, () => {
console.log(`App listening on port ${port}`);
});
当我到达 http://localhost:3030/proxy/https://maps.disasters.nasa.gov/ags04/rest/services/ca_fires_202008/sentinel2/MapServer?f=json
时,我到达了我的目标 json 没问题,access-control-allow-origin: *
正确地加入了。
在我的前端 html(arcgis-js-api 应用程序)中,我调用相同的 url:
var layer = new MapImageLayer({
url: 'http://localhost:3030/proxy/https://maps.disasters.nasa.gov/ags04/rest/services/ca_fires_202008/sentinel2/MapServer?f=json',
});
我的网络选项卡显示的响应不是预期的 JSON,而是 cors-anywhere 代理文本的响应:
对于熟悉 arcgis-js-api 的人,您还可以预先配置代理的使用:
urlUtils.addProxyRule({
urlPrefix: 'maps.disasters.nasa.gov',
proxyUrl: 'http://localhost:3030/proxy/',
});
如果我这样做,网络选项卡显示对 localhost:3030/proxy/<url>
的调用返回 index.html 页面,而不是所需的 json.
为什么当我直接通过浏览器访问 url 时代理给出 expected/required 结果,但从我的前端文件调用时却没有?感谢阅读。
我检查了浏览器控制台并注意到 url 被发送到代理而不是这个
http://localhost:3030/proxy/https://maps.disasters.nasa.gov/ags04/rest/services/ca_fires_202008/sentinel2/MapServer?f=json
看起来像这样
http://localhost:3030/proxy/https:/maps.disasters.nasa.gov/ags04/rest/services/ca_fires_202008/sentinel2/MapServer?f=json
不确定为什么会这样,但作为快速修复,您可以更换
req.url = req.url.replace('/proxy/', '/');
和
req.url = req.url.replace('/proxy/https:/', '/https://');
我正在尝试使用 node、express 和 cors-anywhere 实例为我的 arcgis-js-api 应用设置代理。我的服务器文件如下所示:
import express from 'express';
import cors from 'cors';
import corsAnywhere from 'cors-anywhere';
const { PORT } = process.env;
const port = PORT || 3030;
var app = express();
let proxy = corsAnywhere.createServer({
originWhitelist: [], // Allow all origins
requireHeaders: [], // Do not require any headers.
removeHeaders: [], // Do not remove any headers.
});
app.use(cors());
app.get('/proxy/:proxyUrl*', (req, res) => {
req.url = req.url.replace('/proxy/', '/');
proxy.emit('request', req, res);
});
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'index.html'));
});
app.listen(port, () => {
console.log(`App listening on port ${port}`);
});
当我到达 http://localhost:3030/proxy/https://maps.disasters.nasa.gov/ags04/rest/services/ca_fires_202008/sentinel2/MapServer?f=json
时,我到达了我的目标 json 没问题,access-control-allow-origin: *
正确地加入了。
在我的前端 html(arcgis-js-api 应用程序)中,我调用相同的 url:
var layer = new MapImageLayer({
url: 'http://localhost:3030/proxy/https://maps.disasters.nasa.gov/ags04/rest/services/ca_fires_202008/sentinel2/MapServer?f=json',
});
我的网络选项卡显示的响应不是预期的 JSON,而是 cors-anywhere 代理文本的响应:
对于熟悉 arcgis-js-api 的人,您还可以预先配置代理的使用:
urlUtils.addProxyRule({
urlPrefix: 'maps.disasters.nasa.gov',
proxyUrl: 'http://localhost:3030/proxy/',
});
如果我这样做,网络选项卡显示对 localhost:3030/proxy/<url>
的调用返回 index.html 页面,而不是所需的 json.
为什么当我直接通过浏览器访问 url 时代理给出 expected/required 结果,但从我的前端文件调用时却没有?感谢阅读。
我检查了浏览器控制台并注意到 url 被发送到代理而不是这个
http://localhost:3030/proxy/https://maps.disasters.nasa.gov/ags04/rest/services/ca_fires_202008/sentinel2/MapServer?f=json
看起来像这样
http://localhost:3030/proxy/https:/maps.disasters.nasa.gov/ags04/rest/services/ca_fires_202008/sentinel2/MapServer?f=json
不确定为什么会这样,但作为快速修复,您可以更换
req.url = req.url.replace('/proxy/', '/');
和
req.url = req.url.replace('/proxy/https:/', '/https://');