从在代理后面运行的网站访问网络服务 [英] Accessing webservices from a website which is running behind a proxy
问题描述
我正在设计一个使用 ember 和 express over node 的网站.它在服务器中运行,例如:SERVER_1.
I am designing a website using ember and express over node. Its running in a server, say: SERVER_1.
我在另一台服务器上运行的 Web 服务很少,例如:SERVER_2.
I have few webservices running in another server, say: SERVER_2.
即:
SERVER_1 中的网站和 SERVER_2 中可用的网络服务
website in SERVER_1 and webservices available in SERVER_2
SERVER_1 位于代理服务器后面.我正在尝试从 SERVER_1 访问网络服务:
SERVER_1 is behind a proxy server. And I am trying to access webservices from SERVER_1:
SERVER_1 =====[PROXY]===> SERVER_2
当我从 SERVER_1 进行 AJAX 网络服务调用时,我收到:
When I make AJAX webservice calls from SERVER_1, I receive:
NetworkError: 500 Internal Server Error
但是,我能够通过浏览器成功检索值.仅通过 AJAX 代码,我正在检索网络 500 错误.
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
并且我能够通过 AJAX 代码和浏览器成功访问所有这些网络服务.
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
我得到 -- NetworkError: 500 Internal Server Error
我想知道从代理服务器后面运行的网站访问第三方网络服务的程序?
附加信息:
已经解决了跨域 web 服务访问问题(网站在一台服务器上运行,而 web 服务在其他一些具有不同端口的不同服务器上运行),我正在使用 http-proxy npm,我的代码是如下:
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'));
});
我的 AJAX 调用:
My AJAX call:
$.ajax({
type: 'GET',
async: false,
url: 'API_URL',
success: function (data) {
alert('success');
},
failure: function(error){
alert('error');
}
});
我已经使用 http-proxy 来处理所有 url 的请求了.
Already I using http-proxy to handle all url's request.
如何在上面的代码中配置我的代理服务器的IP地址和端口,以便我可以成功访问所有这些网络服务?或者有什么方法可以在 AJAX 调用中设置我的代理配置?
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?
有人可以指导我吗?
谢谢
推荐答案
通常你需要同时做到这两点:
Usually you need to do both of these :
npm config set proxy your-proxy-address:portnpm 配置集注册表 "http://registry.npmjs.org/"
npm config set proxy your-proxy-address:port npm config set registry "http://registry.npmjs.org/"
这篇关于从在代理后面运行的网站访问网络服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!