【发布时间】:2017-01-30 19:11:33
【问题描述】:
我正在努力思考如何在 Meteor Methods 中向客户端隐藏秘密服务器代码。 The docs seem to imply 以下是其完成方式的一般模式。 https://guide.meteor.com/structure.html#using-require
(请注意,根据文档,我使用 require 而不是 import,因为 import 不能在条件块中使用。)
首先,这是一个在名为 methods.js 的文件中定义的方法,并在客户端和服务器上都导入:
/* methods.js */
let ServerCode
if (Meteor.isServer) {
ServerCode = require('/imports/server-code.js')
}
Meteor.Methods({
'myMethodName': function() {
// ... common code
if (Meteor.isServer) {
ServerCode.secretFunction()
}
// ... common code
}
})
其次,这是/imports/server-code.js 中的秘密服务器代码,我试图不发送给客户端:
/* server-code.js */
class ServerCode {
secretFunction() {
console.log('TOP SECRET!!!!')
}
}
const ServerCodeSingleton = new ServerCode()
module.exports = ServerCodeSingleton
但是当我检查发送到客户端浏览器的源时,我仍然看到我的秘密服务器代码被发送到客户端:
即使我进行生产构建,我仍然可以搜索并找到“绝密!!”细绳。我觉得我对 require 工作原理的理解过于天真,但 Meteor 文档让它看起来如此简单。那么隐藏从 Meteor Methods 调用的密码的正确方法是什么?
【问题讨论】:
标签: javascript meteor require