RESTful api 的 CRUD 方法和端点未在代理 api 服务器中生成
CRUD methods and endpoints for RESTful api not being generated in proxy api server
我正在尝试代理服务器(http://www.swapi.co/api/starships for practice, then a Salesforce api for production). This will be the mobile backend for a React Native app. I'm following the documentation here: http://loopback.io/doc/en/lb2/REST-connector.html#resource-operations。但是,当使用生成器创建使用 CRUD 操作和 Starship 模型的 'starship' 数据源时,我尝试时没有任何显示使用 built-in 浏览器探索 api。代理 RESTful API 时,我想使用 RESTful API 公开它,这甚至可以用于 Loopback 吗?
这是我用来查看 api:
的资源管理器的屏幕截图
以下是我正在执行的步骤:
$ 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。关于我可能做错了什么的任何想法?以下是我能找到的生成文件的内容:
common/models/starship.js
'use strict';
module.exports = function (Starship) {
};
common/models/starship.json
{
"name": "Starship",
"plural": "starships",
"base": "Model",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
server/datasources.json
{
"db": {
"name": "db",
"connector": "memory"
},
"starship": {
"name": "starship",
"baseURL": "http://www.swapi.co/api/starships",
"crud": true,
"connector": "rest"
}
}
server/model-config.json
{
...
"Starship": {
"datasource": "starship",
"public": true
}
}
问题是,您使用的是模型而不是 PersistedModel 作为基础。
模型没有远程访问方法。
您需要将 common/models/starship.json 更改为
{
"name": "Starship",
"plural": "starships",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
查看 http://apidocs.strongloop.com/loopback/#persistedmodel 了解更多信息。
我正在尝试代理服务器(http://www.swapi.co/api/starships for practice, then a Salesforce api for production). This will be the mobile backend for a React Native app. I'm following the documentation here: http://loopback.io/doc/en/lb2/REST-connector.html#resource-operations。但是,当使用生成器创建使用 CRUD 操作和 Starship 模型的 'starship' 数据源时,我尝试时没有任何显示使用 built-in 浏览器探索 api。代理 RESTful API 时,我想使用 RESTful API 公开它,这甚至可以用于 Loopback 吗?
这是我用来查看 api:
的资源管理器的屏幕截图以下是我正在执行的步骤:
$ 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。关于我可能做错了什么的任何想法?以下是我能找到的生成文件的内容:
common/models/starship.js
'use strict';
module.exports = function (Starship) {
};
common/models/starship.json
{
"name": "Starship",
"plural": "starships",
"base": "Model",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
server/datasources.json
{
"db": {
"name": "db",
"connector": "memory"
},
"starship": {
"name": "starship",
"baseURL": "http://www.swapi.co/api/starships",
"crud": true,
"connector": "rest"
}
}
server/model-config.json
{
...
"Starship": {
"datasource": "starship",
"public": true
}
}
问题是,您使用的是模型而不是 PersistedModel 作为基础。
模型没有远程访问方法。
您需要将 common/models/starship.json 更改为
{
"name": "Starship",
"plural": "starships",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
查看 http://apidocs.strongloop.com/loopback/#persistedmodel 了解更多信息。