Accessing webservices from a website which is runn

2020-03-01 17:08发布

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

4条回答
放荡不羁爱自由
2楼-- · 2020-03-01 17:46

Usually you need to do both of these :

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

查看更多
ら.Afraid
3楼-- · 2020-03-01 17:59

In general I suggest you try to get this problem solved by isolating the issue more.

There are still too many open variables. Does your request really travels through the proxy or does it die in the proxy? If it leaves the proxy - why not build just [Proxy] -> Server2?

I have a gut feeling that the issue is somewhere in the (missing) headers that the proxy forwards. This "NetworkError: 500" is - for example - created by PHP-servers when the process dies while sending output. Maybe you trigger a bug in Server2, because your proxy output is different to Server1-output?

Just compare the output of the Proxy against the output of Server1 (nc is your friend). Hope this helps.

查看更多
forever°为你锁心
4楼-- · 2020-03-01 18:08

I found that, my proxy routed API URL is causing trouble here. My proxy server blocks all this routed URL's.

As I am unable to find answer for routed proxy API calls behind a proxy server, I made a temporary patch fix as follows:

Removed the existing proxy routing code from app server (which I used before to solve cross domain AJAX issue):

var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');

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(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'));
});

Now, my AJAX call will be:

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

To handle cross-domain issue in AJAX calls, I made a server side fix by adding 'Access-Control-Allow-Origin' header in all API responses from server.

Some additional info: If anyone struggling with cross-domain issue in AJAX calls for IE8 and IE9 browsers, refer: https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest

Thank You

查看更多
戒情不戒烟
5楼-- · 2020-03-01 18:12

If Sun Tzu was on this thread, he'd tell you the correct strategy would not to be in this position in the first place. As port 443 is usually open on all corporate networks, to allow https traffic consider creating a server using (someone's) cloud offering somewhere and ssh'ing to it on port 443. You could even use this technique to create your own proxy for development purposes (see squidd) - that way at least you could at least see the proxy logs.

You are trying to solve two problems at once - one is networking the other is the code you are writing. Doing this would mean you only have to solve one at a time, and neither is blocking your progress.

查看更多
登录 后发表回答