您可以创建一个“远程方法” http://docs.strongloop.com/display/public/LB/Remote+methods在模型的 JS 文件中,这会“在运行时”添加 API 挂钩,尽管它是在启动时进行的。也就是说,我认为您可以随时使用相同的函数添加端点,而不仅仅是在启动时(尽管我从未尝试过):
Inside /common/models/MyModel.js
module.exports = function(MyModel){
// This is the endpoint for creating endpoints...
MyModel.addEndpoint = function(name, method, cb) {
// audit name and method...
MyModel[name] = function(options, newCb) {
// do whatever this endpoint should do...
newCb(null, 'New endpoint success!');
};
MyModel.remoteMethod(
name,
{
accepts: [{arg: 'options', type: 'object'}], // or whatever you need...
returns: {arg: 'message', type: 'string'}, // whatever it returns...
http: {verb: method}
}
);
cb(null, 'Success creating new endpoint!');
};
MyModel.remoteMethod(
'addEndpoint',
{
accepts: [
{arg: 'name', type: 'string', http: {source: 'body'}},
{arg: 'method', type: 'string', http: {source: 'body'}}
],
returns: {arg: 'message', type: 'string'},
http: {verb: 'post'}
}
);
};