使用环回在URL中获取参数的远程方法

Dev*_*evz 7 node.js loopbackjs

我正在尝试使用环回2.0向我的api添加get远程方法,以实现与默认方法相同的方法结构,例如:

/myObject/{id}
Run Code Online (Sandbox Code Playgroud)

我尝试过的方法是:

  MyObject.remoteMethod(
    'remotemethod', {
      http: {
        path: '/',
        verb: 'get'
      },
      accepts: [
        {arg: 'id', type: 'string'},
      ],
      returns: {
        arg: 'status',
        type: 'string'
      }
    }
  )
Run Code Online (Sandbox Code Playgroud)

但这只允许我这样做:

http://localhost:3000/api/myObject?id=1
Run Code Online (Sandbox Code Playgroud)

有谁知道我怎么能做到这一点?

有人还知道我如何向该路线添加说明以在浏览器中显示吗?文档并没有真正说明太多。.我认为他们的文档不完整,我是唯一一个有这种想法的人吗?

小智 8

回答回送3.0(但我认为它适用于2.0)

MyObject.remoteMethod(
    'remotemethod', {
      description: 'This will insert the description',
      http: {
        path: '/:id',
        verb: 'get'
      },
      accepts: [
        {arg: 'id', type: 'number', required: true},
      ],
      returns: {
        arg: 'status',
        type: 'string'
      }
    }
  )
Run Code Online (Sandbox Code Playgroud)

诀窍是在id参数中添加必填属性,并将参数包含在路径中。

另请参见有关如何添加说明的示例

我确实必须同意这些文档还很不完整。


kun*_*eek 0

您可以单独注释所需的每个参数。

例如

MyObject.remoteMethod(
    'remotemethod', {
      http: {
        path: '/',
        verb: 'get'
      },
      accepts: [
        {arg: 'id', type: 'string', http: {source: query}},
        {arg: 'arg2', type: 'anything', http: {source: query}}
        ......
      ],
      returns: {
        arg: 'status',
        type: 'string'
      }
    }
  )
Run Code Online (Sandbox Code Playgroud)