You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
863 B

  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 parseUrl = require('parseurl');
  13. var qs = require('qs');
  14. /**
  15. * @param {Object} options
  16. * @return {Function}
  17. * @api public
  18. */
  19. module.exports = function query(options) {
  20. var opts = Object.create(options || null);
  21. var queryparse = qs.parse;
  22. if (typeof options === 'function') {
  23. queryparse = options;
  24. opts = undefined;
  25. }
  26. if (opts !== undefined && opts.allowPrototypes === undefined) {
  27. // back-compat for qs module
  28. opts.allowPrototypes = true;
  29. }
  30. return function query(req, res, next){
  31. if (!req.query) {
  32. var val = parseUrl(req).query;
  33. req.query = queryparse(val, opts);
  34. }
  35. next();
  36. };
  37. };