REST客户端¶
注解
要在客户端上不使用Feathers直接使用Feathers REST API(通过HTTP), 请参阅 HTTP API 部分.
@feathersjs/rest-client¶
$ npm install @feathersjs/rest-client --save
@feathersjs/rest-client 允许使用 jQuery, request, Superagent, Axios 要么 Fetch 作为AJAX 连接到通过 express.rest() 公开的服务
小技巧
REST客户端服务确实发出 created, updated, patchched 和 removed 事件, 但只在本地为自己的实例 发出.来自其他客户端的实时事件只能通过websocket连接接收.
注解
客户端应用程序只能使用单个传输(REST, Socket.io 或 Primus).通常不需要在同一客户端应用程序中使用两个传输.
rest([baseUrl])¶
可以通过加载 @feathersjs/rest-client 并使用基本URL初始化客户端对象来初始化REST客户端服务:
const feathers = require(‘@feathersjs/feathers’);
const rest = require(‘@feathersjs/rest-client’);
const app = feathers();
// Connect to the same as the browser URL (only in the browser)
const restClient = rest();
// Connect to a different URL
const restClient = rest(‘http://feathers-api.com’)
// Configure an AJAX library (see below) with that client
app.configure(restClient.fetch(window.fetch));
// Connect to the ``http://feathers-api.com/messages`` service
const messages = app.service(‘messages’);
小技巧
在浏览器中,基本URL与注册服务的位置相关.这意味着 http://api.feathersjs.com/api/v1/messages 的服务,其基本URL为 http://api.feathersjs.com 将作为 app.service('API/V1/messages').使用 http://api.feathersjs.com/api/v1 的基本URL,它将是 app.service('messages').
params.headers¶
请求特定标头可以通过服务调用中的 params.headers:
app.service('messages').create({
text: 'A message from a REST client'
}, {
headers: { 'X-Requested-With': 'FeathersJS' }
});
params.connection¶
允许传递特定于AJAX库的其他选项. params.connection.headers 将与 params.headers 合并:
app.configure(restClient.request(request));
app.service('messages').get(1, {
connection: {
followRedirect: false
}
});
使用 fetch 分叉 yetch 它也可以用来中止请求:
const yetch = require('yetch');
const controller = new AbortController();
app.configure(restClient.fetch(yetch));
const promise = app.service('messages').get(1, {
connection: {
signal: controller.signal
}
});
promise.abort();
jQuery¶
将jQuery($)的实例传递给 restClient.jquery:
app.configure(restClient.jquery(window.jQuery));
或者使用模块加载器:
import $ from 'jquery';
app.configure(restClient.jquery($));
Request¶
需要将 request 对象明确传递给 feathers.request. 使用 request.defaults - 创建一个新的请求对象 - 是一种设置默认标头或身份验证信息的好方法:
const request = require('request');
const requestClient = request.defaults({
'auth': {
'user': 'username',
'pass': 'password',
'sendImmediately': false
}
});
app.configure(restClient.request(requestClient));
Superagent¶
Superagent 目前使用默认配置:
const superagent = require('superagent');
app.configure(restClient.superagent(superagent));
Fetch¶
Fetch 还使用默认配置:
// In Node
const fetch = require('node-fetch');
app.configure(restClient.fetch(fetch));
// In modern browsers
app.configure(restClient.fetch(window.fetch));
HTTP API¶
您可以使用任何其他HTTP REST客户端与Feathers REST API进行通信.以下部分描述了HTTP方法,正文和查询参数属于哪个服务方法调用.
URL中的所有查询参数都将在服务器上设置为 params.query.其他服务参数可以通过以下方式设置 Express. URL查询参数值始终为字符串.转换(例如字符串 'true' 至 布尔 true)也可以在钩子中完成.
POST, PUT 和 PATCH 请求的正文类型由 Express body-parser 确定中间件必须在任何服务之前注册*.您还应该确保将 Accept 标题设置为 application/json.
认证¶
验证HTTP(REST)请求是一个两步过程.首先,您必须通过POST您要使用的策略从 认证 获取JWT:
{
"strategy": "local",
"email": "your email",
"password": "your password"
}
这是curl的样子:
curl -H "Content-Type: application/json" -X POST -d '{"strategy":"local","email":"your email","password":"your password"}' http://localhost:3030/authentication
然后,为了验证后续请求,将返回的 accessToken 添加到 Authorization 标头:
curl -H "Content-Type: application/json" -H "Authorization: <your access token>" -X POST http://localhost:3030/authentication
find¶
从服务中检索所有匹配资源的列表
GET /messages?status=read&user=10
将在服务器上调用 messages.find({query:{status:'read',user:'10'}}).
如果你想使用任何内置的查找操作数($le,$lt,$ne,$eq,$in 等),一般格式如下:
GET /messages?field[$operand]=value&field[$operand]=value2
例如,要查找字段 status 不等于 active 的记录,您可以这样做:
GET /messages?status[$ne]=active
可以找到有关官方数据库适配器的可能参数的更多信息 查询.
get¶
从服务中检索单个资源.
GET /messages/1
将在服务器上调用 messages.get(1,{}).
GET /messages/1?fetch=all
将在服务器上调用 messages.get(1,{query:{fetch:'all'}}).
create¶
使用 data 创建一个新资源,它也可以是一个数组.
POST /messages
{ "text": "I really have to iron" }
messages.create({ "text": "I really have to iron" }, {}) 将在服务器上调用.
POST /messages
[
{ "text": "I really have to iron" },
{ "text": "Do laundry" }
]
update¶
完全替换单个或多个资源.
PUT /messages/2
{ "text": "I really have to do laundry" }
将在服务器上调用 messages.update(2, { "text": "I really have to do laundry" }, {}).当通过直接向端点发送请求时没有给出 id,例如:
PUT /messages?complete=false
{ "complete": true }
将在服务器上调用 messages.update(null, { "complete": true }, { query: { complete: 'false' } }).
小技巧
通常希望 update 替换整个资源,这就是数据库适配器仅支持多个记录的 patch 的原因.
patch¶
使用新的 data 合并单个或多个资源的现有数据.
PATCH /messages/2
{ "read": true }
将在服务器上调用 messages.patch(2, { "read": true }, {}).当没有通过直接向端点发送请求时给出 id:
PATCH /messages?complete=false
{ "complete": true }
将在服务器上调用 messages.patch(null, { complete: true }, { query: { complete: 'false' } }) 以更改所有读取消息的状态.
这是由Feathers 数据库适配器 开箱即用的
remove¶
删除一个或多个资源:
DELETE /messages/2?cascade=true
将调用 messages.remove(2,{query:{cascade:'true'}}).
当通过直接向端点发送请求时没有给出 id,例如:
DELETE /messages?read=true
将调用 messages.remove(null,{query:{read:'true'}}) 删除所有读取消息.


