node.js - 在环回模型中隐藏_所有_远程方法

标签 node.js rest loopback

我正在使用环回3。 我的模型的 js (survey.js) 中有这行代码:

let enabledRemoteMethods = []

Survey.sharedClass.methods().forEach(function(method) {
  console.log(method.name, method.isStatic)
  let matchingEnabledRemoteMethod = _.find(enabledRemoteMethods, {name: method.name});

  if (!matchingEnabledRemoteMethod) {
    Survey.disableRemoteMethodByName(method.name, method.isStatic);
  }
});

它有效......几乎。我仍然可以在资源管理器中看到“PATCH/surveys/{id}”的 REST 端点。我的期望是:资源管理器中不应列出任何 REST 端点。

然后我检查了该操作对应的URL,它是:

http://localhost:3000/explorer/#!/Survey/Survey_prototype_patchAttributes

enter image description here

根据文档,这意味着 patchAttributes 是一个静态方法。

然后我交叉检查了控制台中的输出...它说:pathAttributes 不是静态的。

不一致!

enter image description here

我什至尝试添加这一行:

Survey.disableRemoteMethodByName("patchAttributes", true);

还有

Survey.disableRemoteMethodByName("patchAttributes", false);

运气不好。

有人可以确认这是否是环回 3 中的错误(我不知道环回 2,还没有检查)?如果这是一个错误,我就不必花时间在它上面,只需等待它被修复即可。但如果这不是一个错误,有人可以指出我的代码中缺少什么吗?

谢谢!

<小时/>

更新:弄清楚如何

通过这一行,我可以摆脱它:

Survey.disableRemoteMethodByName("prototype.patchAttributes", true);

第二个参数似乎并不重要(你也可以设置 false)。但不知道为什么(我想它应该只接受 true )。

这是我当前的解决方案:

let disabledPrototypesRemoteMethods = ['patchAttributes']
let enabledRemoteMethods = [
  "create", "findById", "replaceById", "deleteById",
  "replaceOrCreateQuestion"
]
Survey.sharedClass.methods().forEach(function(method) {
  if (enabledRemoteMethods.indexOf(method.name) == -1) {
    Survey.disableRemoteMethodByName(method.name);
  }

  if (disabledPrototypesRemoteMethods.indexOf(method.name) > -1) {
    Survey.disableRemoteMethodByName("prototype." + method.name);
  }
});

仍然有一个小细节:这个东西仍然出现(我想它为 ReplaceById 操作的正常 PUT 提供了 POST 替代方案...,但我不想要它;我想强制用户使用我的 API仅与 PUT 一起使用):

http://localhost:3000/explorer/#!/Survey/Survey_replaceById_post_surveys_id_replace

enter image description here

我尝试添加这一行:

Survey.disableRemoteMethodByName("replaceById_post_surveys_id_replace");

运气不好。

无论如何...希望这对其他人有用;环回文档有点粗略。

<小时/>

最佳答案

您还可以通过查看 stringName 属性来获取原型(prototype)方法。这样您就可以将原型(prototype)包含在列表中。

stringName 在值中包含共享类名称,因此您需要将其解析出来。

module.exports = BusinessProfileContacted => {
  const enabledRemoteMethods = ["create", "findById", "replaceById", "deleteById", "replaceOrCreateQuestion", "prototype.replaceAttributes"];

  Survey.sharedClass.methods().forEach(method => {
    const methodName = method.stringName.replace(/.*?(?=\.)/, '').substr(1);
    if (enabledRemoteMethods.indexOf(methodName) === -1) {
      Survey.disableRemoteMethodByName(methodName);
    }
  });
};

关于node.js - 在环回模型中隐藏_所有_远程方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41898306/

相关文章:

node.js - 是否可以在bunyan日志消息中使用级别值而不是数字?

javascript - Moment.js 周数的日期不正确

web-services - REST 服务 - 暴露非数据 "actions"

loopbackjs - 环回 4 : Upload multipart/form-data via POST method

javascript - 使用 Jade 和 express 访问对象内的 json 对象

git - 在 2 个日期时间之间在 Azure DevOps 中使用 Get Commit API 时,它返回的结果比预期多吗?

java - Jersey 2 - ContainerRequestFilter 获取方法注解

javascript - 环回在 header 中使用标记

java - JAVA 中的环回替代方案

javascript - 在哪里可以找到 nodejs/javascript 客户端组合示例,显示数据从服务器加载到客户端、修改并发送回?