SIGN IN SIGN UP
restify / node-restify UNCLAIMED

The future of Node.js REST development

0 0 63 JavaScript
---
title: Formatters API
permalink: /docs/formatters-api/
---
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
### Table of Contents
2018-04-20 11:44:05 -07:00
- [Usage](#usage)
- [Types](#types)
- [formatter](#formatter)
- [Included formatters](#included-formatters)
- [formatText](#formattext)
- [formatJSON](#formatjson)
- [formatJSONP](#formatjsonp)
- [formatBinary](#formatbinary)
## Usage
Restify comes bundled with a selection of useful formatters that prepare your
responses for being sent over the wire, but you are free to include your own!
```js
function formatGraphQL(req, res, body) {
var data = body;
/* Do a thing to data */
res.setHeader('Content-Length', Buffer.byteLength(data));
return data;
}
var server = restify.createServer({
formatters: {
'application/graphql': formatGraphQL
}
});
// Your application now supports content-type 'application/graphql'
```
## Types
### formatter
Format a response for being sent over the wire
2018-04-20 11:44:05 -07:00
Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)
**Parameters**
2018-04-20 11:44:05 -07:00
- `req` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the request object (not used)
- `res` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the response object
- `body` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** response body to format
2018-04-20 11:44:05 -07:00
Returns **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** formatted response data
## Included formatters
restify comes pre-loaded with a standard set of formatters for common
use cases.
### formatText
Formats the body to 'text' by invoking a toString() on the body if it
exists. If it doesn't, then the response is a zero-length string.
**Parameters**
2018-04-20 11:44:05 -07:00
- `req` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the request object (not used)
- `res` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the response object
- `body` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** response body. If it has a toString() method this
will be used to make the string representation
2018-04-20 11:44:05 -07:00
Returns **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** data
### formatJSON
JSON formatter. Will look for a toJson() method on the body. If one does not
exist then a JSON.stringify will be attempted.
**Parameters**
2018-04-20 11:44:05 -07:00
- `req` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the request object (not used)
- `res` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the response object
- `body` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** response body
2018-04-20 11:44:05 -07:00
Returns **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** data
### formatJSONP
JSONP formatter. like JSON, but with a callback invocation.
Unicode escapes line and paragraph separators.
**Parameters**
2018-04-20 11:44:05 -07:00
- `req` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the request object
- `res` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the response object
- `body` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** response body
2018-04-20 11:44:05 -07:00
Returns **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** data
### formatBinary
Binary formatter.
**Parameters**
2018-04-20 11:44:05 -07:00
- `req` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the request object
- `res` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the response object
- `body` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** response body
2018-04-20 11:44:05 -07:00
Returns **[Buffer](https://nodejs.org/api/buffer.html)** body