extend the CRUD method in LoopBack

烂漫一生 提交于 2019-12-12 18:19:09

问题


I would like to know how to extend CRUD methods created by LoopBack.

I have a model with an attribute public_key. I would like to build two custom behaviors for the POST api endpoint for this model.

  1. generate a public key and set the value
  2. generate a private key and send it back as a result (using SSL)

How can I extend the default method to implement these behaviors?


回答1:


I was able to override the default method by creating a javascript file under server/boot.

module.exports = function(app) {
  var MyModel = app.models.MyModel;
  var create = MyModel.create;

  // Overrides POST '/api/MyModel' endpoint
  MyModel.create = function(data, done) {
    // Do custom things

    create.call(MyModel, data, done);
  };
};

However I was not able to modify the return value.




回答2:


You can define custom remote methods that are exposed as REST endpoints



来源:https://stackoverflow.com/questions/34623902/extend-the-crud-method-in-loopback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!