strongloop loopback:如何将远程方法添加到 loopback-component-storage 容器

strongloop loopback: how to add remote method to loopback-component-storage Container

我正在尝试向 loopback-component-storage 添加一个新的 REST API 方法,用于从容器下载所有照片。 (参见

以下代码似乎有效,但我想知道如何在 strongloop 框架内正确加载存储处理程序 handler 和文件系统提供程序 factory。另外,我不必复制 datasources.json

中的数据

有什么建议吗?

// http://localhost/api/containers/container1/downloadContainer/IMG_0799.PNG

// file: ./server/models/container.js
loopback_component_storage_path = "../../node_modules/loopback-component-storage/lib/";

datasources_json_storage = {
  "name": "storage",
  "connector": "loopback-component-storage",
  "provider": "filesystem",
  "root": "svc/storage",
  "_options": {
    "getFileName": "",
    "allowedContentTypes": "",
    "maxFileSize": "",
    "acl": ""
  }
};

handler = require(loopback_component_storage_path + './storage-handler');

factory = require(loopback_component_storage_path + './factory');

module.exports = function(Container) {
  Container.downloadContainer = function(container, files, res, ctx, cb) {
    var provider;
    provider = factory.createClient(datasources_json_storage);
    return handler.download(provider, null, res, container, files, cb);
  };
  Container.remoteMethod('downloadContainer', {
    shared: true,
    accepts: [
      {arg: 'container', type: 'string', 'http': {source: 'path'}},
      {arg: 'files', type: 'string', 'http': {source: 'path'}},
      {arg: 'res', type: 'object', 'http': {source: 'res'}}
    ],
    returns: [],
    http: {
      verb: 'get',
      path: '/:container/downloadContainer/:files'
    }
  });

container.js 模型文件中,您可以轻松访问您在 datasources.json 中定义的 StorageService。一旦你有了它,你就可以调用它的 download() 方法。无需在您的代码中实例化(冗余地和每个请求)factoryhandler

service = Container.app.dataSources.{SourceName}.connector
service.download(container, file, res, cb)