Connect
JSON
Parse JSON request bodies, providing the
parsed object as req.body.
Options
strictwhenfalseanythingJSON.parse()accepts will be parsedreviverused as the second "reviver" argument for JSON.parselimitbyte limit disabled by default
Source
exports = module.exports = function(options){
var options = options || {}
, strict = options.strict === false
? false
: true;
var limit = options.limit
? _limit(options.limit)
: noop;
return function json(req, res, next) {
if (req._body) return next();
req.body = req.body || {};
// check Content-Type
if ('application/json' != utils.mime(req)) return next();
// flag as parsed
req._body = true;
// parse
limit(req, res, function(err){
if (err) return next(err);
var buf = '';
req.setEncoding('utf8');
req.on('data', function(chunk){ buf += chunk });
req.on('end', function(){
if (strict && '{' != buf[0] && '[' != buf[0]) return next(utils.error(400, 'invalid json'));
try {
req.body = JSON.parse(buf, options.reviver);
next();
} catch (err){
err.body = buf;
err.status = 400;
next(err);
}
});
});
}
};
noop()
noop middleware.
Source