在loopback nodejs中搜索api

时间:2018-02-08 08:48:50

标签: node.js loopbackjs

我需要在loopback nodejs中实现这个api

https://myapi.com/part_numbers?part_number=1245,6787,89490,940044...

关于如何去做的任何想法? part_number之后的路径应该接受至少100个partnumbers,然后返回结果。

我在环回会话中看到的所有文档都只发送一个get请求到https://myapi.com/part_numbers?part_number=1245之类的内容 但不是要发送多个逗号分隔值

关于如何使用loopback和nodejs构建此端点的任何想法我使用mysql作为后端数据存储区。

1 个答案:

答案 0 :(得分:1)

免责声明:我是LoopBack的维护者,也是pull请求的共同作者,在输入参数中添加了对字符分隔数组的支持。

LoopBack提供了一个功能标志,用于在输入参数中启用逗号分隔的数组值,此标志由https://github.com/strongloop/strong-remoting/pull/142添加。 (不幸的是,它还没有记录。)

如何使用它:

1)在server/config.json中配置允许的分隔符:

{
  "restApiRoot": "/api",
  // ...
  "rest": {
    "handleErrors": false,
    // ADD THE FOLLOWING LINE
    "arrayItemDelimiters": [","],
    // original content - keep it around:
    "normalizeHttpPath": false,
    "xml": false
  },
  // ...
}

2)定义一个自定义远程方法(参见docs),接受“数字数组”类型的参数。例如,假设您已经定义了一个名为PartNumbers的模型:

PartNumbers.get = function(numbers) {
  // replace this code with your implementation
  // here, we simply return back the parsed array
  return Promise.resolve(numbers);
};

PartNumbers.remoteMethod('get', {
  accepts: {
    arg: 'part_numbers',
    type: ['number'],
    required: true,
  },
  // modify "returns" to match your actual response format
  returns: {
    arg: 'data',
    type: 'object',
    root: true,
  },
  http: {verb: 'GET', path: '/'},
});

3)启动您的应用程序,在http://localhost:3000/explorer打开API资源管理器并为您的新方法提供帮助!