例如:为了创建新用户,标准路径为POST
/api/users/
。
这将调用create
远程钩子,等等。
怎么可能将标准路径更改为类似的东西,POST
/api/users/new/
并仍然保留当前(正确的)功能?谢谢。
另外,是否可以使用新的远程方法user.new()复制此功能?看起来怎么样?
添加一个远程方法user.new
并User.create()
自己在其中调用。
我假设您从(小写)user.js和user.json模型开始,该模型是回送附带的内置(大写)User模型的扩展。
在user.js中,如下所示:
module.exports = function(user) {
user.remoteMethod('new',
{
accepts: [
{arg: 'userInfo', type: 'object'}
],
returns: {
arg: 'success',
type: 'boolean'
}
}
);
user.new = function(userInfo, cb) {
user.create(userInfo, function(err, newUser) {
if(err) return cb(err, null);
return cb(null, true);
});
};
};
您还可以http
使用远程方法规范上的其他属性来修改REST API http url路径结构。这不是严格必需的,因为该方法默认情况下将采用该方法的名称。如果您可以修改它以覆盖内置行为,那将很酷,但是我还没有测试这是否可行。您还可以强制执行POST:
user.remoteMethod('new',
{
http: {path: '/new', verb: 'post'}, // <--
accepts: [
{arg: 'userInfo', type: 'object'}
],
returns: {
arg: 'success',
type: 'boolean'
}
}
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句