首页 文章

如何覆盖默认的回送模型API路径

提问于
浏览
0

我们如何覆盖默认的loopback REST API模型 endpoints ?例如,我想在调用以下GET API时调用名为 list 的自定义模型方法 .

enter image description here

我指的是文档https://loopback.io/doc/en/lb2/Exposing-models-over-REST.html

1.环回资源管理器的API endpoints :http://localhost:3000/api/Assets

2.Model方法定义:

Asset.list = function(cb) {
    console.log("called");
}

Asset.remoteMethod('list', {
    http: {path: '/'},
    returns: {type: 'Object', root: true}
});

1 回答

  • 0

    您的 console.log("called"); 应仅出现在您的终端中,而不是在您的网络浏览器上显示 - 此时_2960703不会看到它 .

    如果要在Web浏览器上看到某些内容,则必须为回调返回一个值,例如:

    module.exports = function (Asset) {
    
        Asset.list = function(cb) {
            console.log("called");
            cb(false, {'called': true}); // cb(error, returned value(s));
        }
    
        Asset.remoteMethod('list', {
            http: {verb: 'get'},
            returns: {type: 'Object', root: true}
        });
    }
    

    此文件应位于common / model / asset.js中


    在您的server / model-config.json中,不要忘记引用您的模型:

    ...
         "Asset": {
            "dataSource": "yourdatasource", //change this by your datasource name
            "public": true
         }
         ...
    

相关问题