无法在node.js上设置代理 [英] cannot set proxy on node.js

查看:94
本文介绍了无法在node.js上设置代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have node.js 4.1.1 and express.js 4.8.5 on port 5550. I also have Geoserver 2.8.0 on port 8080. Both servers are on the same laptop.

节点上的应用想要访问Geoserver中的一些地图数据,这些是openlayers上的详细信息

An app on node wants to access some map data from Geoserver and these are the details on 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 不可用).为了避免CORS和访问控制允许起源错误,我想在Node上设置代理.我只想使用Node,因为它更易于设置.

Setting CORS or proxy on Geoserver is not possible cause of technical problems (old Jetty core, hack-ish solutions on the wild, unavailiable jars for old Jetty version). To avoid CORS and Access Control Allow Origins errors I want to set a proxy on Node. I just want to use Node because its easier to set.

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

According to this and a previous question here I have to set a reverse proxy, so

  • 我不在客户端上进行代理配置
  • Geoserver是通过Node反向代理提供服务的,因此看起来像它们起源相同(=没有更多的CORS问题)
  • 客户端希望访问Geoserver,但是可以通过Node来访问,而无需知道了

我想我的概念正确,但是我不知道如何实现.我选择了 http-proxy-middleware 来执行此操作.我在我的 app.js

I guess I got the concept right, but I dont know how to implement this. I chose the http-proxy-middleware to do this. I added on my 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'标头在请求的资源上.因此,不允许访问源"http://localhost:5550".响应的HTTP状态代码为404.

我不明白如何实现这一目标.请指出我的错误,或者如果我没有正确理解该概念.请帮助我理解.

I fail to understand how to implement this. Please point out my errors or if I did not get the concept right. Please help me understand.

谢谢

更新

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

These are the headers I see when I open the browser's console

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,而不是您的快递服务器.

以Geoserver为目标的常规语法:

Normal syntax with Geoserver as target:

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

或使用简写语法:

此配置的行为与上一个完全相同.

This configuration behaves exactly as the previous one.

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

这篇关于无法在node.js上设置代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆