sendme
v0.1.7
Published
Express middleware used to convert json and res.send in multiple formats
Maintainers
Readme
Sendme ExpressJS Middleware
ExpressJS middleware. Send response containing data. Format in JSON, XML, csv, xlsx, html, and text.
Install
$ npm install sendmeUsage
var sendme = require('sendme');
app.use(sendme(config)); // Attaches sendme(data) to res.
app.use(function(req, res) {
var json;
// Do work.
res.sendme(json);
});require('sendme')(config)
config (optional) is a JSON object containing formatting configurations. The config is passed to the delimit() function in tabular-json. Pass format-specific options using a sub-object (e.g. as {xlsx: {includeHeaders: false}}), and non-specific options to the base config object.
Config Examples
var config = {
includeHeaders: false,
sort: ['id'],
txt: {
separator: ';',
stringWrap: '"'
}
};See tabular-json for more details on config options
Formats
sendme will send the response in various formats. Either specify a content type using the Accept header, or append a format extension to the end of the req.path. A format extension will always override the Accept header.
.json- Content-Types:application/jsonapplication/jsonis the primary response type.
.xml- Content-Types:application/xml,text/xml.xlsx- Content-Types:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.csv- Content-Types:application/csv.html- Content-Types:text/html.txt- Content-Types:text/plain
Examples
Specify a format in the url path:
GET /foos/1.xml HTTP/1.1
Host: example.comvar data = {
id: 1,
type: 'widget'
};
res.sendme(data);Response:
HTTP/1.0 200 OK
Content-Type: text/xml
<?xml version="1.0" encoding="UTF-8" ?>
<data>
<id>1</id>
<type>widget</type>
</data>Also, specify a format in the Accept parameter:
GET /foos HTTP/1.1
Host: example.com
Accept: application/csvResponse:
HTTP/1.0 200 OK
Content-Type: application/csv
"id","type"
1,"widget"
2,"gadget"