Arbitrary response content types in Feathers

十年热恋 提交于 2019-12-12 13:14:14

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/40169827/arbitrary-response-content-types-in-feathers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!