Thursday, March 12, 2015

node quick hit: url pass through

So, the jQuery in your node based app can't reach out to other sites directly because of browser's same-origin policy.

Enter "request"

Assuming you're using express (After doing "npm install request --save"):

app.get('/url/:url', function(req,res){
  request.get(req.params.url).pipe(res)
});

and my jQuery was something like

$.get("/url/"+encodeURIComponent("http://somesite.com/etc/"),function(data){
console.log(data);
    });}

I guess it's not much worse than JSONP... anyway, this is just running in a trusted environment.



No comments:

Post a Comment