Skip to content

Instantly share code, notes, and snippets.

@TyrfingMjolnir
Forked from anonymous/gist:59b9d5c3e41658c63f70
Last active August 29, 2015 14:09
Show Gist options
  • Save TyrfingMjolnir/db6c0dd8e0f318bb1935 to your computer and use it in GitHub Desktop.
Save TyrfingMjolnir/db6c0dd8e0f318bb1935 to your computer and use it in GitHub Desktop.
var o2x = require( 'object-to-xml' );
var j2c = require( 'json-2-csv' );
var j2t = require( 'json-2-tsv' );
function( req, res, next ) {
if( req.accepts( 'application/json' ) ) {
return res.json( req.payload );
}
if( req.accepts( 'text/xml' ) ) {
res.set( 'content-type', 'text/xml' );
return res.send( o2x( req.payload ) );
}
if( req.accepts( 'text/csv' ) ) {
req.set( 'content-type', 'text/csv' );
return res.send( j2c( req.payload ) );
}
if( req.accepts( 'text/tsv' ) ) {
req.set( 'content-type', 'text/tsv' );
return res.send( j2t( req.payload ) );
}
console.log( req.headers.accept );
return res
.status( 406 )
.send( 'Not Acceptable' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment