扩展 LoopbackJS 端点方法

Extend a LoopbackJS endpoint method

我想在环回中扩展用户登录 POST 方法。

到目前为止,我已经扩展了基本用户 class 以推出我自己的用户,但是如何向特定端点添加功能?

在此示例中,我创建了一个名为 "UserAuth2" 的新模型,它扩展了 LoopbackJS 提供的现有用户模型。我使用 slc loopback:model 工具创建了模型。

为了在 Loopback 中扩展函数,请在模型的 JS 文件中使用以下代码:

module.exports = function(UserAuth2) {
   // Get reference to endpoint
   var previousImplementation = UserAuth2.login;

   // Create new implementation of endpoint
   UserAuth2.login = function(){

   //Get existing implementation
   /*** arguments is an array of existing arguments that the login 
      function takes***/

   previousImplementation.apply(this, arguments);

   //Extend the method and do something else here
   console.log("New functionality");
   }
}