【发布时间】:2015-10-05 15:38:46
【问题描述】:
我在端口 5550 上有 node.js 4.1.1 和 express.js 4.8.5。我还在端口 8080 上有 Geoserver 2.8.0。两台服务器都在同一台笔记本电脑上。
节点上的应用想要从 Geoserver 访问一些地图数据,这些是 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 版本不可用的jars)。为了避免 CORS 和 Access Control Allow Origins 错误,我想在 Node.js 上设置一个代理。我只想使用 Node,因为它更容易设置。
- 我不在客户端做代理配置
- 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] Proxy created: /geoserver -> http://localhost:5550
但我仍然收到错误Image from origin 'http://localhost:8080' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:5550' is therefore not allowed access. The response had HTTP status code 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
【问题讨论】:
-
类似问题:gis.stackexchange.com/questions/4323/… 我需要一些时间来尝试使用 nodejs 进行反向代理。
-
您应该使用不同的代理库:stackoverflow.com/questions/22889417/… 他们有超过 1000 颗星,而您的库只有大约 25 颗星...这里有一些关于其中一个库的教程:blog.ccare.me/blog/2013/09/18/… 试试出来吧!
标签: node.js express proxy reverse-proxy