Gui Prá
Gui Prá

Reputation: 5677

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?

Upvotes: 3

Views: 2423

Answers (1)

Daff
Daff

Reputation: 44215

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.

Upvotes: 2

Related Questions