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?
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With