1 | /*!
|
2 | * Connect - query
|
3 | * Copyright(c) 2011 TJ Holowaychuk
|
4 | * Copyright(c) 2011 Sencha Inc.
|
5 | * MIT Licensed
|
6 | */
|
7 |
|
8 | /**
|
9 | * Module dependencies.
|
10 | */
|
11 |
|
12 | var parseurl = require('parseurl');
|
13 | var qs = require('@sailshq/qs');
|
14 |
|
15 | /**
|
16 | * Query:
|
17 | *
|
18 | * Automatically parse the query-string when available,
|
19 | * populating the `req.query` object using
|
20 | * [qs](https://github.com/visionmedia/node-querystring).
|
21 | *
|
22 | * Examples:
|
23 | *
|
24 | * connect()
|
25 | * .use(connect.query())
|
26 | * .use(function(req, res){
|
27 | * res.end(JSON.stringify(req.query));
|
28 | * });
|
29 | *
|
30 | * @param {Object} options
|
31 | * @return {Function}
|
32 | * @api public
|
33 | */
|
34 |
|
35 | module.exports = function query(){
|
36 | return function query(req, res, next){
|
37 | if (!req.query) {
|
38 | req.query = ~req.url.indexOf('?')
|
39 | ? qs.parse(parseurl(req).query, { allowDots: false, allowPrototypes: true })
|
40 | : {};
|
41 | }
|
42 |
|
43 | next();
|
44 | };
|
45 | };
|