strongloop loopback:如何向loopback-component-storage容器添加远程方法 [英] strongloop loopback: how to add remote method to loopback-component-storage Container

查看:173
本文介绍了strongloop loopback:如何向loopback-component-storage容器添加远程方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向回送组件存储中添加新的REST API方法,以从Container下载所有照片. (请参见

I'm trying to add a new REST API method to the loopback-component-storage for downloading all photos from a Container. (see Strongloop loopback-storage-service: how to use StorageService.downloadStream() to download a ZIP of all photos in container?)

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

The following code seems to work, but I'd like to know how to load the storage-handler, handler, and filesystem provider, factory, correctly within the strongloop framework. Also, I shouldn't have to copy the data in 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.

From within the container.js model file, you can easily access the StorageService you have defined in datasources.json. Once you have that, you can just call its download() method. No need to instantiate (redundantly and with every request) factory or handler in your code.

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

这篇关于strongloop loopback:如何向loopback-component-storage容器添加远程方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆