重新定义端点的 Strongloop 内置方法
Redefine Strongloop built in methods for endpoint
我知道如何像这样创建自定义远程方法:
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'}
});
});
有了这个URL:
localhost:3000/api/dogs/myMethod?age=3&owner=joe
但我需要这样称呼它:
localhost:3000/api/dogs?age=3&owner=joe
我在 Strongloop API 文档中找到这篇文章(在 "Change the implementation of built-in methods" 部分,"Via your model's script" 小节):https://docs.strongloop.com/display/public/LB/Customizing+models
但它没有说明如何处理自定义参数。
没给我答案
感谢您的帮助!
好的,我找到路了。
我所要做的就是添加这一行:
Dog.disableRemoteMethod('find', true); // Removes (GET) /dog
在此之前(我还必须将“/list”更改为“/”以使其工作):
Dog.remoteMethod('myMethod', {
http: {path: '/', verb: 'get'},
...
)};
所以现在我可以像这样向 URL 发出请求:localhost:3000/api/dogs?age=3&owner=joe
整个代码是:
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'}
});
});
我知道如何像这样创建自定义远程方法:
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'}
});
});
有了这个URL:
localhost:3000/api/dogs/myMethod?age=3&owner=joe
但我需要这样称呼它:
localhost:3000/api/dogs?age=3&owner=joe
我在 Strongloop API 文档中找到这篇文章(在 "Change the implementation of built-in methods" 部分,"Via your model's script" 小节):https://docs.strongloop.com/display/public/LB/Customizing+models
但它没有说明如何处理自定义参数。
感谢您的帮助!
好的,我找到路了。
我所要做的就是添加这一行:
Dog.disableRemoteMethod('find', true); // Removes (GET) /dog
在此之前(我还必须将“/list”更改为“/”以使其工作):
Dog.remoteMethod('myMethod', {
http: {path: '/', verb: 'get'},
...
)};
所以现在我可以像这样向 URL 发出请求:localhost:3000/api/dogs?age=3&owner=joe
整个代码是:
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'}
});
});