Calling a server-ony method from client

My question seems simple, but I can’t figure out the answer.

I have a form with just one input which is a website url. From that URL, I need to parse the HTML and get the HTML element.

To do that, when the form is submit, I call a Meteor method that does the job. It works on the server, I get the title from the URL, but not on the client because of a cross origin issue (CORS). I guess in that case, the best is to only execute the method server side, but I understood this is not possible using Meteor Methods because they always run both on server and client for Optimistic UI. So the solution would be to call a simple javascript function in a server folder, but how can I call it from the client, when the form is submited? Or maybe I should use another patern?

Thanks.

Methods on the client are optional, and you can call a server only methods.

Template.Form.events({
 'submit form'(event, instance) {
   var url = 'get the url';
   Meteor.call('processHTML', url, function(err, res) {
    if(err) console.error(err);
    else console.log(res);
   });
 }
});

Just make sure the method is defined in a file in the server folder, to ensure it only runs on the server.

I don’t have my computer now so I can’t test, but can I import and call a method define in a server folder from my client file ?

Shouldn’t I use isSimulation or isClient as stated in the Meteor Guide to load secret code on the client (even if it is not secret in that case)? Here is the part I am refering to in the guide: https://guide.meteor.com/security.html#secret-code

If it’s a method that you sometimes use on the client as well as on the server, I’d just make it a plain function in a file used on both client and server. Simply call the function on the client and, if you want to use it on the server, send the necessary parameters to a server method and call the same function using those parameters.

Using isClient or isSimulation in a method works too. I’ve occasionally used those to work around client-side weirdness when I don’t have the time or inclination to debug/refactor.

I ended up using Meteor.isServer to be sure the HTTP request is not executed in client and avoid CORS issue. Thank you for your help. :wink: