Accessing webservices from a website which is running behind a proxy
Asked Answered
R

4

9

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

Reservation answered 2/12, 2013 at 7:56 Comment(6)
How about using a forward proxy?Dactylography
Lots of objects in Node.js don't throw error but instead emit error events. Just listen to the error event and console.log it, so that we know what's happening.Dactylography
I tried with forward proxy. However, it haven't helped me. Can you please guide me?Reservation
Setting NPM configuration has nothing to do with your problem. As far as I understood, you have a local server S1 that is trying to access external urls S2 behind a proxy: [ S1 ] ==== [ proxy ] ===> [ S2 ] With library are you using to request S2 from S1 ? Unless you're consumming those REST Web Service directly from the browser in Ajax ?Apiculate
Thanks for your reply Feugy.. I am using http and http-proxy libraries in S1 to access webservices (which are running in S2). And you are right, S1 is behind a proxy server. If I remove this proxy server, I can access webservices successfully. the codes are provided in 'Additional Info' section. My issue is, when I have a proxy server.. I am getting trouble in accessing webservices.Reservation
Feugy, I have also edited my question. Hope now its clear. Sorry!Reservation
G
3

Usually you need to do both of these :

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

Guildroy answered 18/1, 2014 at 23:8 Comment(6)
I tried both these.. however, it haven't helped. As I said before, I can install all npm packages (which means my node server can connect to internet and download all packages). However, if I am calling those 3rd party webservices behind my corporate proxy, I am getting such error. So I feel that I should have some code in my website node settings (with both my Proxy IP and port).. can you please guide me still further?Reservation
If all else fails you can always configure the host as whole to use the the proxy server. Is it a windows or linux host? You should first make sure the proxy is really your problem first logging onto the (node server) host and using traceroute to the address of the webservice you are trying to consume should show you where the packets stop on your network. wget --proxy-user=USERNAME --proxy-password=PASSWORD path.to.domain.com/some.html . Will let you directly try and access the endpoint from the proxy. WhenGuildroy
Thanks for the reply Darren White. I have edited my question. Can you please provide your thoughts on that?Reservation
You need to execute the network commands I suggested. You're not working in a sandbox - as others suggested you need to isolate your problem. Your browser is clearly working with the proxy setting. Is anything else on your host? If your host is behind a proxy, how are you updating or patching from the Internet? Are those apps using a proxy correctly as well?Guildroy
Thanks for the reply Darren. It really guiding me. I feel that only my [PROXY] server is blocking all API calls (the requests are not crossing proxy server). I found this by following scenario: Initially there is no proxy server between client and server. I was able to access API successfully. When I introduce proxy server between client and server, I am getting this error. Hence, I feel the proxy server is blocking my API request. I feel, I have to somehow configure my proxy server's IP and port in client code to access API.Reservation
Also, I confirmed this through my proxy server's squid - access log. First I accessed API from a browser (by calling API URL). I received response successfully and there was a log entry in squid for this API call. Now, I tried the same API call through my AJAX code, now there is no response and no entry in squid logs. I feel, my squid proxy server is blocking or not allowing these api calls. Can you please guide me further?Reservation
I
2

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.

Innes answered 24/1, 2014 at 7:6 Comment(6)
I feel that my [PROXY] server is blocking all API calls (these API requests are not crossing proxy server). I confirmed this through my proxy server's squid - access log. First I accessed API from a browser (by calling API URL). I received response successfully and there was a log entry in squid for this API call. Now, I tried the same API call through my AJAX code, now there is no response and no entry in squid logs. I feel, my squid proxy server is blocking or not allowing these api calls. Can you please guide me further?Reservation
Hmmmm... Good news seems the issue is completely under your control. See your code where you call the proxy. I don't know this module, but you do not return any value nor hand over a call back. Maybe something wrong there? Poor people debugger: console.log maybe helps?Innes
Sorry I am too new to this node-http-proxy module. Already to fix the cross-domain AJAX issue, I am using RoutingProxy (please review my code found in 'Additional Info' section. Now I feel, I need to somehow include my proxy server's ip and port to have a successful API access from my client to server. can you say where to debug my code?Reservation
Excellent - post your squid configuration :)Guildroy
You might want to completely eliminate everything node by using the proxy setting for the ENTIRE HOST- something like export http_proxy=server-ip:port. Then you can use wget or telnet to the service you are using to confirm that the proxy is blocking, rather than that anything in you app is misconfigured.Guildroy
Sorry, I actually have no access/control to review squid configurations in my office :( Can't we handle this in node server or expressJS? so that, my AJAX call/API request contains my proxy server's IP and port configuration and they cross this proxy server?Reservation
G
2

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.

Guildroy answered 26/1, 2014 at 12:12 Comment(0)
R
0

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

Reservation answered 30/1, 2014 at 5:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.