简体   繁体   English

我是否必须将方法用于服务器端专用逻辑?

[英]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. 我想知道是否仅在服务器端进行操作,是否需要使用Meteor.methods ,如果没有,则将逻辑放在哪里。

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 ). 在客户端,我不能让一个第三方API调用对延迟补偿,或者使用使用节点模块Npm.require (假设不使用meteorhacks:npm )。 So I always wrap those methods around if(! this.isSimulation) . 因此,我总是将这些方法包装在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目录下的server全局函数。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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