Tag: meteor

To make HTTP request to remote services, we have to use Meteor.http package.
Meteor.http package can be added to project by command “meteor add http”.

Meteor.http supports both synchronous and asynchronous requests. But in this example/usecase, we want to return results to client. And Meteor by default supports synchronous services on server (which conflicts with node.js asynchronous model), so I will use synchronous Meteor.http api in this example.

var url = 'http://.....';
var result = Meteor.http.get(url, {timeout:30000});
if(result.statusCode==200) {
    var respJson = JSON.parse(result.content);
    console.log("response received.");
    //custom code
    if(respJson.message.code == 0) { //IF OK
        console.log('returning response');
        return respJson;
    } else {
        throw new Meteor.Error(respJson.message.code, respJson.message.text);
    }
} else {
    console.log("Content issue: ", result.statusCode);
    throw new Meteor.Error("");
}

Full source code is available at https://gist.github.com/2922814
Live demo (twitter): http://remoteservice.meteor.com