Yes, you can call directly from the server to the web service in order to receive data. I am not so sure how you did for the Meteor.methods/Meteor.call and say it did not work. But basically, the idea is client will click the button and then button will trigger a method on the server. The server method then will call the web service and return the data.
Some example code could be:
Template['template'].events({
'click .getData': function(event: any) {
Meteor.call('serverMethod', function(err, res) {
if (err) {alert(err);}
else { ... }
)
}
});
The tricky part for new comer when calling the rest is you need to use aysnc calling in order to return the data to client. We normally make helper function for that
public static get(url: string, headers: any): any {
var httpCall = Meteor.wrapAsync(HTTP.call);
var result = httpCall('GET', url, {headers: headers});
if (result.statusCode == 200) {
try {
var res = JSON.parse(result.content);
return res;
} catch(err) {
return result.content;
}
}
return null;
}
And call the helper like this
public static serverMethod(username: string, password: string): any {
var response = RestService.get(query.url, query.header);
return response;
}
This way, the res in the client code above will get the result.
I actually dropped the Angular 2 for the lack of documentation, but stay with Typescript for my system because I can wrap all the meteor call inside the Typescript class, as you can see in my example, serverMethod is in the typescript function format, not in meteor way like Meteor.methods({....}), which is really good for now
Typically, this is a class in my server folder
// server/rest.service.ts
declare var RestService: any;
RestService = class RestService {
methodMap = {
"getFromRest": RestService.get,
"postToRest": RestService.post,
};
constructor() {
var abstractService = new AbstractService();
abstractService.registerMethod(this.getClassName(), this.methodMap);
}
getClassName(): string {
return this.constructor.toString().match(/\w+/g)[1];
}
//------------------------------------------------------------------------------------
// Helper methods
//------------------------------------------------------------------------------------
public static get(url: string, headers: any): any {
var httpCall = Meteor.wrapAsync(HTTP.call);
var result = httpCall('GET', url, {headers: headers});
if (result.statusCode == 200) {
try {
var res = JSON.parse(result.content);
return res;
} catch(err) {
return result.content;
}
}
return null;
}
I have a class to map the typescript service to the meteor method
// server/abstract.service.ts
declare var AbstractService: any;
AbstractService = class AbstractService {
constructor() {}
public registerMethod (scopeName: string, methodMap: {[key:string]:any}) {
var scopeMap: {[key:string]: any} = {};
for (var key in methodMap) {
scopeMap[scopeName + '.' + key] = methodMap[key];
}
Meteor.methods(scopeMap);
}