Jan Eliáš Jan Eliáš - 28 days ago 18
Javascript Question

Redefine Strongloop built in methods for endpoint

I know how to create custom remote method like this:

module.exports = function (Dog) {
Dog.myMethod = function(age, owner, cb);

Dog.find({where: {age: age, owner: owner}}, function(err, result) {

var myResult = new Array();

// transform "result" to "myResult"
cb(null, myResult);
});


Dog.remoteMethod('myMethod', {
http: {path: '/list', verb: 'get'},
accepts: [
{arg: 'age', type: 'number', http: {source: 'query'}},
{arg: 'owner', type: 'string', http: {source: 'query'}}
],
returns: {arg: 'list', type: 'json'}
});

});


With this URL:

localhost:3000/api/dogs/myMethod?age=3&owner=joe

But i need to call it like this:

localhost:3000/api/dogs?age=3&owner=joe

I found in Strongloop API documentation this article (in section "Change the implementation of built-in methods", subsection "Via your model's script"): https://docs.strongloop.com/display/public/LB/Customizing+models

But it doesnt show how to handle custom parameters.

StrongLoop overriding PUT built in method didn't give me the answer.

Thank you for any help!

Answer

Ok, I found the way.

All I had to do was to add this line:

Dog.disableRemoteMethod('find', true); // Removes (GET) /dog

Before this (I also had to change "/list" to "/" to make it work):

Dog.remoteMethod('myMethod', {
   http: {path: '/', verb: 'get'},
   ...
)};

So now I can make request to URL like this: localhost:3000/api/dogs?age=3&owner=joe

Whole code is:

module.exports = function (Dog) {
    Dog.myMethod = function(age, owner, cb);

        Dog.find({where: {age: age, owner: owner}}, function(err, result) {

          var myResult = new Array();

          // transform "result" to "myResult"
          cb(null, myResult);
        });


    Dog.disableRemoteMethod('find', true); // Removes (GET) /dog

    Dog.remoteMethod('myMethod', {
        http: {path: '/', verb: 'get'},
        accepts: [
           {arg: 'age', type: 'number', http: {source: 'query'}},
           {arg: 'owner', type: 'string', http: {source: 'query'}}
        ],            
        returns: {arg: 'list', type: 'json'}
    });

});
Comments