简体   繁体   中英

Do I have to use method for server-side only logic?

I would like to know if I have to use Meteor.methods if I am only doing server-side operations, and if not, where to put the logic.

I find myself doing this a lot:

Meteor.methods({
  someMethod: function () {
    if (! this.isSimulation) {
      // make third party api call, use node module, etc.
    }
  }
});

On the client side, I cannot make a third party API call to latency compensate, or use node module using Npm.require (assuming not using meteorhacks:npm ). So I always wrap those methods around if(! this.isSimulation) .

But it seems unnecessary to put this logic in a method if I cannot use latency compensation. What is the widely adopted practice? Do I put those logic in a method still?

You can just declare your pure server-side logic as server global functions living under the server directory.

server/lib/logic.js

serverLogic = function(){
  // your server-side only logic 
};

Then you can simply call your function anywhere in the server environment, inside a method, a collection hook, etc...

server/anywhere.js

serverLogic();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM