gpt4 book ai didi

node.js - 无法在node.js上设置代理

转载 作者:太空宇宙 更新时间:2023-11-03 23:35:36 24 4
gpt4 key购买 nike

我在端口 5550 上有 node.js 4.1.1 和express.js 4.8.5。我还在端口 8080 上有 Geoserver 2.8.0。两台服务器都在同一台笔记本电脑上。

Node 上的应用程序想要从 Geoserver 访问一些 map 数据,这些是 openlayers 上的详细信息

 source: new ol.source.TileWMS({
url: 'http://localhost:8080/geoserver/mymap/wms?',
crossOrigin: 'anonymous',
// I also tried crossOrigin: 'localhost:8080/' and crossOrigin: 'localhost:5550/' but nothing

params: {'LAYERS': 'mymap:layer, mymap:anotherLayer, FORMAT': 'image/png' ,'CRS': 'EPSG:3857'},
serverType: 'geoserver'

在 Geoserver 上设置 CORS 或代理不可能导致技术问题(旧的 Jetty 核心、野外的 hack-ish 解决方案、旧 Jetty 版本不可用的 jar)。为了避免 CORS 和访问控制允许来源错误,我想在 Node 上设置代理。我只是想使用 Node,因为它更容易设置。

根据this和上一个问题 here我必须设置反向代理,所以

  • 我不在客户端上进行代理配置
  • Geoserver 通过 Node 反向代理获得服务,所以看起来它们具有相同的起源(=不再有 CORS 问题)
  • 客户端想要访问 Geoserver,但通过 Node 进行访问,但没有知道了

我想我的概念是正确的,但我不知道如何实现它。我选择了http-proxy-middleware去做这个。我添加了我的 app.js

var proxyMiddleware = require('http-proxy-middleware'); 

var proxy = proxyMiddleware('http://localhost:8080/geoserver', {
target: 'http://localhost:5550',
changeOrigin: true
});

var app = express();

app.use('/' , function (req, res) {
res.render('index', { title: 'testing', head: 'Welcome Testing Area'});
});

app.use(proxy);
app.listen(5550);

在控制台上我看到[HPM]已创建代理:/geoserver -> http://localhost:5550

但我仍然收到错误来自源“http://localhost:8080”的图像已被跨源资源共享策略阻止加载:请求的资源上不存在“Access-Control-Allow-Origin” header 。因此,不允许访问源“http://localhost:5550”。响应的 HTTP 状态代码为 404。

我不明白如何实现这个。请指出我的错误或者如果我没有正确理解这个概念。请帮助我理解。

谢谢

更新

这些是我打开浏览器控制台时看到的标题

General
Remote Address:[::1]:8080
Request URL:http://localhost:8080/geoserver/mymap/wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&LAYERS=mymap%3Aplanet_osm_polygon%2C%20mymap%3Aplanet_osm_line%2C%20mymap%3Aplanet_osm_roads%2C%20mymap%3Aplanet_osm_point&TILED=true&CRS=EPSG%3A3857&WIDTH=256&HEIGHT=256&STYLES=&BBOX=2269873.9919565953%2C4618019.500877209%2C2348145.508920616%2C4696291.017841229
Request Method:GET
Status Code:404 Not Found

Response Headers
HTTP/1.1 404 Not Found
Content-Type: text/html; charset=iso-8859-1
Content-Length: 1408
Server: Jetty(6.1.8)

Request Headers
Accept:image/webp,image/*,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:el-GR,el;q=0.8,en;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:5550
Referer:http://localhost:5550/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36

最佳答案

看来您错误配置了代理。

  • 混合使用普通代理配置和速记配置
  • 目标应该是 Geoserver,而不是您的 Express 服务器。

以 Geoserver 作为目标的正常语法:

var proxy = proxyMiddleware('/geoserver', {
target: 'http://localhost:8080',
changeOrigin: true
});

或者使用简写语法:

此配置的行为与前一个配置完全相同。

var proxy = proxyMiddleware('http://localhost:8080/geoserver', {
changeOrigin: true
});

关于node.js - 无法在node.js上设置代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32952476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com