-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
31 lines (29 loc) · 851 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"use strict";
/**
* pretty json middleware.
*
* - `always`: prettify response without query, if it's specified true. (default: false)
* - `query`: query-string parameter name for pretty response. (default: none)
* - `spaces`: pretty print spaces. (default: 2)
*
* @param {object} option
* @return {function}
*/
module.exports = function(option) {
option = option || {};
var always = option.always || false;
var query = option.query;
var spaces = option.spaces || 2;
return function(req, res, next) {
if (always === true || typeof req.query[query] !== 'undefined') {
res.json = function(body) {
// content-type
if (!res.get('Content-Type')) {
res.set('Content-Type', 'application/json');
}
return res.send(JSON.stringify(body, null, spaces));
}
}
next();
}
};