Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arbitrary response content types in Feathers

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?

like image 465
Gui Prá Avatar asked Dec 06 '25 16:12

Gui Prá


1 Answers

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.

like image 165
Daff Avatar answered Dec 08 '25 10:12

Daff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!