query.js 863 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. };