从正在运行代理的网站访问网络服务 [英] Accessing webservices from a website which is running behind a proxy

查看:164
本文介绍了从正在运行代理的网站访问网络服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个使用ember并通过节点快照的网站。它在服务器上运行,说:SERVER_1。



我在其他服务器上运行的Web服务很少,比如:SERVER_2。



即:SERVER_1中的



网站和SERVER_2中提供的网络服务



SERVER_1在代理服务器后面。我正在尝试从SERVER_1访问webservices:

  SERVER_1 ===== [PROXY] ===> SERVER_2 

当我从SERVER_1进行AJAX webservice调用时,我收到:

  NetworkError:500内部服务器错误

但是,我可以通过浏览器成功检索值。只有通过AJAX代码,我正在检索网络500错误。



同样为了测试,我删除了我的代理服务器设置:

  SERVER_1 =====> SERVER_2 

,我可以通过AJAX代码和浏览器成功访问所有这些Web服务。 p>

如果我们之间有一个代理服务器:

  SERVER_1 == === [PROXY] ===> SERVER_2 

我收到 - NetworkError:500内部服务器错误 p>

我喜欢知道从代理服务器后面的网站访问第三方网络服务的过程。



附加信息



已经修复跨域Web服务访问问题(网站在一个服务器中运行, web服务在其他不同的服务器上运行不同的端口),我使用的是 http-proxy npm,我的代码如下:

  var express = require('express'); 
var routes = require('./ routes');
var http = require('http');
var path = require('path');
var httpProxy = require('http-proxy');
var endpoint = {
host:'IP_ADDRESS_WHERE_MY_WEBSERVICES_RUN',
port:80,
前缀:'/ api'
}

var proxy = new httpProxy.RoutingProxy();
var app = express();

app.set('port',process.env.PORT || 3000);
app.set('views',path.join(__ dirname,'views'));
app.set('view engine','ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.methodOverride());
app.use(express.cookieParser('xxxxx'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__ dirname,'public')));
app.use(function(req,res){
if(req.url.indexOf(endpoint.prefix)=== 0){
proxy.proxyRequest(req,res,endpoint );
}
});
app.use(express.bodyParser());

if('development'== app.get('env')){
app.use(express.errorHandler());
}
app.get('/',routes.index);

http.createServer(app).listen(app.get('port'),function(){
console.log('Express server listening on port'+ app.get 'port'));
});

我的AJAX调用:

  $。ajax({
type:'GET',
async:false,
url:'API_URL',
success:function(data) {
alert('success');
},
failure:function(error){
alert('error');
}
} );

已经使用http代理来处理所有url的请求。



如何在上述代码中配置代理服务器的IP地址和端口,以便我可以成功访问所有这些Web服务?还是有一些方法可以在AJAX调用中使用我的代理配置?



任何人都可以指导我?



谢谢

解决方案

通常你需要做这两个:



npm config set proxy your-proxy-address:port
npm config set registry http://registry.npmjs .org /


I am designing a website using ember and express over node. Its running in a server, say: SERVER_1.

I have few webservices running in another server, say: SERVER_2.

That is:

website in SERVER_1 and webservices available in SERVER_2

SERVER_1 is behind a proxy server. And I am trying to access webservices from SERVER_1:

SERVER_1 =====[PROXY]===> SERVER_2

When I make AJAX webservice calls from SERVER_1, I receive:

NetworkError: 500 Internal Server Error

However, I am able to retrieve values successfully through browser. Only through AJAX code, I am retrieving Network 500 error.

Also for testing, I removed my proxy server setup:

SERVER_1 =====> SERVER_2

and I was able to access all those web services successfully both via AJAX code and browser.

If I have a proxy server in between them:

SERVER_1 =====[PROXY]===> SERVER_2

I am getting -- NetworkError: 500 Internal Server Error

I like to know the procedures to access third-party webservices from a website which is running behind a proxy server?

Additional Info:

Already to fix cross domain web-service access issue (website running in one server and webservices running in some other different servers with different ports), I am using http-proxy npm and my code is as follows:

var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var httpProxy = require('http-proxy');
var endpoint  = {
    host:   'IP_ADDRESS_WHERE_MY_WEBSERVICES_RUN',
    port:   80,
    prefix: '/api'
}

var proxy = new httpProxy.RoutingProxy();
var app = express();

app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.methodOverride());
app.use(express.cookieParser('xxxxx'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req, res) {
    if (req.url.indexOf(endpoint.prefix) === 0) {
        proxy.proxyRequest(req, res, endpoint);
    }
});
app.use(express.bodyParser());

if ('development' == app.get('env')) {
    app.use(express.errorHandler());
}
app.get('/', routes.index);

http.createServer(app).listen(app.get('port'), function(){
    console.log('Express server listening on port ' + app.get('port'));
});

My AJAX call:

$.ajax({
            type: 'GET',
            async: false,
            url: 'API_URL',
            success: function (data) {
                alert('success');
            },
            failure: function(error){
                alert('error');
            }
        });

Already I using http-proxy to handle all url's request.

How can I configure my proxy server's IP address and port in the above code, so that I can access all these webservices successfully? Or is there some way to have my proxy configurations in AJAX call?

Can anyone please guide me?

Thank you

解决方案

Usually you need to do both of these :

npm config set proxy your-proxy-address:port npm config set registry "http://registry.npmjs.org/"

这篇关于从正在运行代理的网站访问网络服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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