羽毛中的任意响应内容类型 [英] Arbitrary response content types in Feathers

查看:82
本文介绍了羽毛中的任意响应内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义服务,必须以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.

这篇关于羽毛中的任意响应内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆