6
votes

I'm an ember beginner, using ember-cli v0.0.47 and struggling to get http-proxy to work.

I am trying to make an ajax request to a remote OGC CSW server. The request is a normal HTTP GET request with some additional parameters and the expected response is an XML document.

Since I am making a cross origin request, I decided to use a server proxy, in order to avoid dealing with CORS stuff.

I have used ember-cli to generate the proxy configuration with:

ember-cli generate http-proxy geoland2 http://geoland2.meteo.pt

In my controller, I have defined a 'search' action that uses jquery.ajax to communicate with the server:

export default Ember.Controller.extend({

    actions: {
        search: function() {
            Ember.$.ajax({
                url: 'geoland2/geonetwork/srv/eng/csw',
                contentType: 'application/xml',
                crossDomain: true,
                xhrFields: {
                    withCredentials: true
                },
                dataType: 'xml',
                data: {
                    service: 'CSW',
                    version: '2.0.2',
                    request: 'GetCapabilities'
                },
            }).then(
                function(data) {
                    alert(data);
                    Ember.$('.results').html(data);
                },
                function(jqXHR, textStatus, errorThrown) {
                    Ember.$('.results').html(jqXHR.status + ' ' + errorThrown + ' - ' + jqXHR.responseText);
                }
            );
        }
    }
});

Now when this action gets triggered, I would expect that the call to

geoland2/geonetwork/srv/eng/csw 

would be proxied by ember-cli's server and sent to

http://geoland2.meteo.pt/geonetwork/srv/eng/csw?service=CSW&version=2.0.2&request=GetCapabilitites

Is this assumption of what should happen correct?

In reality what I see is that the request is not proxied at all. The ember app tries to contact

http://localhost:4200/geoland2/geonetwork/srv/eng/csw?service=CSW&version=2.0.2&request=GetCapabilitites

and it fails with a 404 HTTP error because the specified resource is obviously not available.

I have edited the autogenerated server/proxies/geoland2.js file by commenting the line that joined the proxyPath variable with the rest of the URL:

var proxyPath = '/geoland2';

module.exports = function(app) {
  // For options, see:
  // https://github.com/nodejitsu/node-http-proxy
  var proxy = require('http-proxy').createProxyServer({});
  var path = require('path');

  app.use(proxyPath, function(req, res, next){
    // include root path in proxied request
    //req.url = path.join(proxyPath, req.url); // I commented this line
    proxy.web(req, res, {target: 'http://geoland2.meteo.pt:80'});
  });
};

This seems right for my use case as my sever's endpoint is at

http://geoland2.meteo.pt/geonetwork/srv/eng/csw

And not

http://geoland2.meteo.pt/geoland2/geonetwork/srv/eng/csw

I believe that even if this change might be wrong, I should be getting back something from the original server.

Do I somehow still need to fix some CORS related issue in order for the proxy to work? Or maybe there are some more files to edit in order to get http-proxy correctly set up?

1
While I can't help you with your particular question, I can tell you that there's no such thing as "the ember server". Everything runs client side in Ember, if you need server-side functionality you need a separate server for that and Ember can't help you create it. The ember-cli server is just to serve the files during development, not for dynamic processing.Leeft
Yes, I understand that. What I mean is the express server that ember-cli uses to serve my site during development. I think this server should proxy my ajax request to the real host and it does not seem to want to do it.Ricardo Garcia Silva
@RicardoGarciaSilva what if you just empty out proxyPath so it is just var proxyPath=''charleetm
I'm getting the exact same 404 problem and I'm not able to overcome it. I thought maybe it was due to this: github.com/nodejitsu/node-http-proxy/issues/720 and github.com/nodejitsu/node-http-proxy/issues/696, and solved with this: github.com/nodejitsu/node-http-proxy/pull/723, but it still doesn't work for me.Johnny Oshika
If all you're worried about is the CORS thing, why not give ember-cli-content-security-policy a try? Works fine out of the box and saves you the hassle of setting up additional proxies.Kiffin

1 Answers

1
votes

In the current versions of ember cli you can start your app using the proxy option pointing to your external server.

ember server --proxy http://externalserver.ccc/api/