query.js 885 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*!
  2. * express
  3. * Copyright(c) 2009-2013 TJ Holowaychuk
  4. * Copyright(c) 2013 Roman Shtylman
  5. * Copyright(c) 2014-2015 Douglas Christopher Wilson
  6. * MIT Licensed
  7. */
  8. 'use strict';
  9. /**
  10. * Module dependencies.
  11. */
  12. var merge = require('utils-merge')
  13. var parseUrl = require('parseurl');
  14. var qs = require('qs');
  15. /**
  16. * @param {Object} options
  17. * @return {Function}
  18. * @api public
  19. */
  20. module.exports = function query(options) {
  21. var opts = merge({}, options)
  22. var queryparse = qs.parse;
  23. if (typeof options === 'function') {
  24. queryparse = options;
  25. opts = undefined;
  26. }
  27. if (opts !== undefined && opts.allowPrototypes === undefined) {
  28. // back-compat for qs module
  29. opts.allowPrototypes = true;
  30. }
  31. return function query(req, res, next){
  32. if (!req.query) {
  33. var val = parseUrl(req).query;
  34. req.query = queryparse(val, opts);
  35. }
  36. next();
  37. };
  38. };