无法在node.js上设置代理

我有端口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核心,在旧的Jetty版本的野生的,不可用的jars上的hack-ish解决scheme)。 为了避免CORS和Access Control Allow Origins错误我想在节点上设置一个代理。 我只是想使用节点,因为它更容易设置。

根据这个和以前的问题,我必须设置一个反向代理,所以

  • 我不在客户端上进行代理configuration
  • Geoserver通过一个节点反向代理服务器,看起来像他们有相同的起源(=没有更多的CORS问题)
  • 客户端想要访问Geoserver,但不知道它通过节点

我想我的概念是正确的,但我不知道如何实现这一点。 我select了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. 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.

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

谢谢

UPDATE

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

 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 

看起来你错误地configuration了代理。

  • 混合使用正常代理configuration和简写configuration
  • 目标应该是Geoserver,而不是你的快递服务器。

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

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

或者用简写的语法:

这个configuration的行为与前一个完全一样。

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

chimurai是对的。 终于为我设置了http-proxy-middleware

在我的app.js我现在有

 var proxyMiddleware = require('http-proxy-middleware'); var proxy = proxyMiddleware('http://localhost:5550', { target: 'http://localhost:8080', changeOrigin: true, xfwd: true }); /* the above can be replaced by chimurai's version : var proxy = proxyMiddleware('/geoserver', { target: 'http://localhost:8080', changeOrigin: true }); and will still work */ var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); app.use(favicon()); app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded()); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', function(req, res, next) { httpProxy.createProxyServer({target:'http://localhost:8080'}); next(); }); app.use(proxy); app.listen(5550); 

我删除了这个crossOrigin: 'anonymous',在我的Openlayers代码中,我修复了Openlayers中的链接错误,现在工作正常。

我也试图通过在Geoserver上设置一个代理来解决这个问题,但这是不可能的,因为Geoserver运行一个旧的Jetty版本,现在是EOL,所以没有官方的代理Geoserver的解决scheme,也没有升级它。

通过Node解决这个问题是最好的解决scheme