【问题标题】:Meteor secret server code -- reference error, variable not definedMeteor 秘密服务器代码——引用错误,变量未定义
【发布时间】:2017-07-26 20:54:55
【问题描述】:

在流星文档 (https://guide.meteor.com/security.html#secret-code) 中的秘密服务器代码部分,他们似乎使用了仅在服务器上定义的全局变量,因此,只能在服务器上查看和访问代码。看起来很简单。

但是当我这样做时

upload = { test: "my secret code" }

在文件夹server/upload.js 内我得到错误

W20170726-10:04:59.843(2)? (STDERR) 
C:\Users\myuser\AppData\Local\.meteor\packages\meteor-tool\1.5.0\mt-os.windows.x86_32\dev_bundle\server-lib\node_modules\fibers\future.js:280
W20170726-10:04:59.844(2)? (STDERR)                                             throw(ex);
W20170726-10:04:59.846(2)? (STDERR)                                             ^
W20170726-10:04:59.847(2)? (STDERR)
W20170726-10:04:59.847(2)? (STDERR) ReferenceError: upload is not defined
W20170726-10:04:59.848(2)? (STDERR)     at meteorInstall.server.upload.upload.js (server/upload/upload.js:1:1)
W20170726-10:04:59.849(2)? (STDERR)     at fileEvaluate (packages\modules-runtime.js:333:9)
W20170726-10:04:59.850(2)? (STDERR)     at require (packages\modules-runtime.js:228:16)
W20170726-10:04:59.851(2)? (STDERR)     at C:\Users\myuser\Documents\projects\myproject\.meteor\local\build\programs\server\app\app.js:10417:1
W20170726-10:04:59.852(2)? (STDERR)     at C:\Users\myuser\Documents\projects\myproject\.meteor\local\build\programs\server\boot.js:338:34
W20170726-10:04:59.853(2)? (STDERR)     at Array.forEach (native)
W20170726-10:04:59.854(2)? (STDERR)     at Function._.each._.forEach (C:\Users\myuser\AppData\Local\.meteor\packages\meteor-tool\1.5.0\mt-os.windows.x86_32\dev_bundle\server-lib\node_modules\underscore\underscore.js:79:11)
W20170726-10:04:59.855(2)? (STDERR)     at C:\Users\myuser\Documents\projects\myproject\.meteor\local\build\programs\server\boot.js:158:5
W20170726-10:04:59.856(2)? (STDERR)     at C:\Users\myuser\Documents\projects\myproject\.meteor\local\build\programs\server\boot.js:387:5
W20170726-10:04:59.858(2)? (STDERR)     at Function.run (C:\Users\myuser\Documents\projects\myproject\.meteor\local\build\programs\server\profile.js:510:12)

是文档有误还是我只是在做一些奇怪的事情?我正在使用流星版本 1.5.0,在 windows 和 linux 上都发生了。

【问题讨论】:

    标签: meteor server hidden


    【解决方案1】:

    文档指出,

    应用中的秘密业务逻辑应位于以下代码中 只加载在服务器上

    它(不幸的是)暗示,meteor 方法或验证方法的代码也作为乐观 UI 的一部分在客户端虚拟执行(请参阅this.isSimulation),因此可能会暴露秘密,例如密钥。

    在这里使用global.myvariable = { ... }not a good solution

    为了让您更清楚,我稍微扩展了文档中的示例:

    /server/mmr.js(仅由您的服务器加载)

    export const MMR = {
      updateWithSecretAlgorithm(userId) {
        // your secret code here
      }
    }
    

    /both/updatemmr.js(由服务器和客户端加载)

    if (Meteor.isServer) {
        //eslint will nag but it does not cause any error
        import {MMR} from '../server/mmr.js';
    }
    
    // In a file loaded on client and server
    const Meteor.users.methods.updateMMR = new ValidatedMethod({
      name: 'Meteor.users.methods.updateMMR',
      validate: null,
      run() {
        if (this.isSimulation) {
          // Simulation code for the client (optional)
        } else {
          MMR.updateWithSecretAlgorithm(this.userId);
        }
      }
    });
    

    Meteor.isServer 只保证客户端不会尝试导入 MMR,这会导致启动时出错。只要您仅在服务器上加载 mmr.js 文件,就不会向客户端公开 MMR 对象。

    我希望这会使示例更加清晰。

    【讨论】:

    • 是的,我确实想尝试这样的事情,但我认为条件导入很脏(正如你提到的 eslint 会唠叨),但也许你是对的,全局变量确实更混乱。但是我确实发现我们可以使用 require 代替 (guide.meteor.com/structure.html#using-require),这似乎是有条件导入的推荐流星方式。
    • 是的,使用 require 更省钱。我只是因为命名导出而使用显式导入,而且我在任何非顶级代码中都没有遇到问题,即使在生产中也没有。另请注意,提到的动态需求问题现在由dynamic imports 解决
    【解决方案2】:

    好的,由于某种原因,只需键入 myvariable = { ... } 就将其添加到全局对象中,因此我使用 global.myvariable = { ... } 明确添加了 i。到目前为止,它似乎运作良好!

    编辑

    正如 Jankapunkt 正确指出的那样,确实不鼓励使用全局变量。但是,不要像 Jankapunkt 建议的那样在 if 语句中使用 import,而是应该使用 require 的 CommonJS 语法,如流星文档 (https://guide.meteor.com/structure.html#using-require) 中建议的那样,例如

    let MMR;
    if (Meteor.isServer) {
       MMR = require('../server/mmr.js').MMR;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-01
      相关资源
      最近更新 更多