Feathers 中的流式响应
Streaming response in Feathers
在 Express 中,流式响应很容易实现,因为 res
是一个流对象。
但是,在 Feathers 自定义服务方法中,如何流式传输某些内容作为响应?
您或许可以实施 custom response format which pipes res.data
if it is a stream, and then resolve your service method with a stream. There is also a discussion about streaming data here。
您可以在特定服务之前或之后添加一个custom service middleware,然后您就可以使用res
。
示例:
const todoService = {
async get(id) {
return {
id,
description: `You have to do ${id}!`
};
}
};
function streamResponse(req, res, next) {
res.write('Streams are awesome!');
res.end;
}
app.use('/todos', todoService, streamResponse);
在 Express 中,流式响应很容易实现,因为 res
是一个流对象。
但是,在 Feathers 自定义服务方法中,如何流式传输某些内容作为响应?
您或许可以实施 custom response format which pipes res.data
if it is a stream, and then resolve your service method with a stream. There is also a discussion about streaming data here。
您可以在特定服务之前或之后添加一个custom service middleware,然后您就可以使用res
。
示例:
const todoService = {
async get(id) {
return {
id,
description: `You have to do ${id}!`
};
}
};
function streamResponse(req, res, next) {
res.write('Streams are awesome!');
res.end;
}
app.use('/todos', todoService, streamResponse);