问题描述
我有一个自定义服务,必须以CSV格式返回数据。
我不能使用标准的Express路由,因为我需要在该端点上安装Feathers的钩子
我找不到Feathers服务返回非HTML,非JSON数据的示例,也找不到指定响应内容类型的方法。 / p>
从服务方法返回之前,使用 res.set('Content-Type','text / csv')
没有工作;即使方法的返回值是常规字符串,最终的 Content-Type
头也会重置为 application / json
。
如何在Feathers的自定义服务方法中正确设置任意响应内容类型?
您可以自定义响应格式,例如:
const feathers = require('feathers');
const rest = require(‘feathers-rest’);
const app = feathers();
函数restFormatter(req,res){
res.format({
'text / plain':function(){
res.end(`Message是: $ {res.data.text}`);
}
});
}
app.configure(rest(restFormatter));
完整文档可在。
使用您的,也应该可以。
I have a custom service that must return data in CSV format.
I can't use a standard Express route, because I need Feathers' hooks on this endpoint.
I couldn't find an example of a Feathers service returning non-HTML, non-JSON data, and have found no way to specify a response content type.
Using res.set('Content-Type', 'text/csv')
before returning from the service method didn't work; the final Content-Type
header was reset to application/json
, even though the method's return value was a regular string.
How can I properly set arbitrary response content types in Feathers' custom service methods?
You can customize the response format like this:
const feathers = require('feathers');
const rest = require('feathers-rest');
const app = feathers();
function restFormatter(req, res) {
res.format({
'text/plain': function() {
res.end(`The Message is: "${res.data.text}"`);
}
});
}
app.configure(rest(restFormatter));
The complete documentation can be found here.
Using your own service specific middleware to send the response should also work.
这篇关于羽毛中的任意响应内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!