Search code examples
httppostloopbackjsstrongloop

How to define post response in StrongLoop / LoopBack


I've recently tried to make a somewhat simple implementation of a POST HTTP handler with LoopBack but didn't manage to. This is the case. When I create a model class it seems that I can only add remote method to implement business functionality. But what if I want to handle the request myself in a proper way in response of a POST request ? It's not very RESTFul to add special methods with names rather than implementing proper handling of a HTTP verb request. Is there any way to achieve that ? Thanks.


Solution

  • You can define your own verb and path for the method as follows:

    MyModel.myMethod = function(...) { ... }
    
    MyModel.remoteMethod('myMethod', {
      accepts: ...,
      returns: ...,
      http: {
        verb: 'post',
        path: '/'
      }
    });
    

    When would attach the myMethod function to the /api/MyModels/ endpoint.

    For further docs on remote methods checkout the following:

    For the 2nd link, a sharedMethod is what gets created when you use the MyModel.remoteMethod(...) function.