/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js
JavaScript | 61 lines | 17 code | 6 blank | 38 comment | 2 complexity | 44ffa34201daa8f9ba8d2aea7be6a140 MD5 | raw file
Possible License(s): Apache-2.0, MIT
1 2/*! 3 * Connect - bodyParser 4 * Copyright(c) 2010 Sencha Inc. 5 * Copyright(c) 2011 TJ Holowaychuk 6 * MIT Licensed 7 */ 8 9/** 10 * Module dependencies. 11 */ 12 13var multipart = require('./multipart') 14 , urlencoded = require('./urlencoded') 15 , json = require('./json'); 16 17/** 18 * Body parser: 19 * 20 * Parse request bodies, supports _application/json_, 21 * _application/x-www-form-urlencoded_, and _multipart/form-data_. 22 * 23 * This is equivalent to: 24 * 25 * app.use(connect.json()); 26 * app.use(connect.urlencoded()); 27 * app.use(connect.multipart()); 28 * 29 * Examples: 30 * 31 * connect() 32 * .use(connect.bodyParser()) 33 * .use(function(req, res) { 34 * res.end('viewing user ' + req.body.user.name); 35 * }); 36 * 37 * $ curl -d 'user[name]=tj' http://local/ 38 * $ curl -d '{"user":{"name":"tj"}}' -H "Content-Type: application/json" http://local/ 39 * 40 * View [json](json.html), [urlencoded](urlencoded.html), and [multipart](multipart.html) for more info. 41 * 42 * @param {Object} options 43 * @return {Function} 44 * @api public 45 */ 46 47exports = module.exports = function bodyParser(options){ 48 var _urlencoded = urlencoded(options) 49 , _multipart = multipart(options) 50 , _json = json(options); 51 52 return function bodyParser(req, res, next) { 53 _json(req, res, function(err){ 54 if (err) return next(err); 55 _urlencoded(req, res, function(err){ 56 if (err) return next(err); 57 _multipart(req, res, next); 58 }); 59 }); 60 } 61};