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 |
|
13 | var multipart = require('./multipart')
|
14 | , urlencoded = require('./urlencoded')
|
15 | , json = require('./json');
|
16 |
|
17 | /**
|
18 | * Body parser:
|
19 | *
|
20 | * Status: the multipart body parser will be removed in Connect 3.
|
21 | *
|
22 | * Parse request bodies, supports _application/json_,
|
23 | * _application/x-www-form-urlencoded_, and _multipart/form-data_.
|
24 | *
|
25 | * This is equivalent to:
|
26 | *
|
27 | * app.use(connect.json());
|
28 | * app.use(connect.urlencoded());
|
29 | * app.use(connect.multipart());
|
30 | *
|
31 | * Examples:
|
32 | *
|
33 | * connect()
|
34 | * .use(connect.bodyParser())
|
35 | * .use(function(req, res) {
|
36 | * res.end('viewing user ' + req.body.user.name);
|
37 | * });
|
38 | *
|
39 | * $ curl -d 'user[name]=tj' http://local/
|
40 | * $ curl -d '{"user":{"name":"tj"}}' -H "Content-Type: application/json" http://local/
|
41 | *
|
42 | * View [json](json.html), [urlencoded](urlencoded.html), and [multipart](multipart.html) for more info.
|
43 | *
|
44 | * If you wish to create your own body parser, you may be interested in:
|
45 | *
|
46 | * - [raw-body](https://github.com/stream-utils/raw-body)
|
47 | * - [body](https://github.com/raynos/body)
|
48 | *
|
49 | * @param {Object} options
|
50 | * @return {Function}
|
51 | * @api public
|
52 | */
|
53 |
|
54 | exports = module.exports = function bodyParser(options){
|
55 | var _urlencoded = urlencoded(options)
|
56 | , _multipart = multipart(options)
|
57 | , _json = json(options);
|
58 |
|
59 | return function bodyParser(req, res, next) {
|
60 | _json(req, res, function(err){
|
61 | if (err) return next(err);
|
62 | _urlencoded(req, res, function(err){
|
63 | if (err) return next(err);
|
64 | _multipart(req, res, next);
|
65 | });
|
66 | });
|
67 | }
|
68 | }; |
\ | No newline at end of file |