在代理api服务器

时间:2016-10-22 15:45:16

标签: javascript rest api loopbackjs

我正在尝试代理服务器(http://www.swapi.co/api/starships进行练习,然后使用Salesforce api进行生产)。这将是React Native应用程序的移动后端。我在这里关注文档:http://loopback.io/doc/en/lb2/REST-connector.html#resource-operations。但是,当使用生成器创建使用CRUD操作和Starship模型的“星舰”数据源时,当我尝试使用内置资源管理器探索api时,没有任何内容显示出来。在代理RESTful API时,我想使用RESTful API公开它,这是否可以使用Loopback?

以下是我用来查看api的浏览器的屏幕截图:

Loopback api explorer screenshot

以下是我要采取的步骤:

$ slc loopback:datasource
? Enter the data-source name: starship
? Select the connector for starship: REST Services (supported by StrongLoop)
? Base URL for the REST service: http://www.swapi.co/api/starship
? Default options for the request: [left blank, hit enter]
? An array of operation templates: [left blank, hit enter]
? Use default CRUD mapping: (y/N) Y

$ slc loopback:model
? Enter model name: Starship
? Select data-source to attach Starship to: starship (rest)
? Select model's base class: Model
? Expose Person via the REST API? (Y/n) Y
? Custom plural form (used to build REST URL): starships
? Common model or server only? common

Let's add some Starship properties now.

Enter an empty property name when done.
? Property name: [left empty, hit enter]

$ npm start

> wfsapi@1.0.0 start /Users/me/projects/wfsapi
> node .

Web server listening at: http://0.0.0.0:3000
Browse your REST API at http://0.0.0.0:3000/explorer

然而,当我导航到资源管理器时,只有用户api出现,而Starships则没有。关于我可能做错的任何想法?以下是我可以找到的生成文件的内容:

公共/模型/ starship.js

'use strict';

module.exports = function (Starship) {

};

公共/模型/ starship.json

{
    "name": "Starship",
    "plural": "starships",
    "base": "Model",
    "idInjection": true,
    "options": {
        "validateUpsert": true
    },
    "properties":  {},
    "validations":  [],
    "relations": {},
    "acls": [],
    "methods": {}
}

服务器/ datasources.json

{
  "db": {
    "name": "db",
    "connector": "memory"
  },
  "starship": {
    "name": "starship",
    "baseURL": "http://www.swapi.co/api/starships",
    "crud": true,
    "connector": "rest"
  }
}

服务器/模型config.json

{ 
  ... 
  "Starship": {
    "datasource": "starship",
    "public": true
  }
}

1 个答案:

答案 0 :(得分:0)

问题是,你使用的是Model而不是PersistedModel。

模型没有远程访问方法。

您需要将您的common / models / starship.json更改为

{{1}}

查看http://apidocs.strongloop.com/loopback/#persistedmodel了解详情。