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
851 B

  1. /*!
  2. * Connect - query
  3. * Copyright(c) 2011 TJ Holowaychuk
  4. * Copyright(c) 2011 Sencha Inc.
  5. * MIT Licensed
  6. */
  7. /**
  8. * Module dependencies.
  9. */
  10. var qs = require('qs')
  11. , parse = require('../utils').parseUrl;
  12. /**
  13. * Query:
  14. *
  15. * Automatically parse the query-string when available,
  16. * populating the `req.query` object.
  17. *
  18. * Examples:
  19. *
  20. * connect()
  21. * .use(connect.query())
  22. * .use(function(req, res){
  23. * res.end(JSON.stringify(req.query));
  24. * });
  25. *
  26. * The `options` passed are provided to qs.parse function.
  27. *
  28. * @param {Object} options
  29. * @return {Function}
  30. * @api public
  31. */
  32. module.exports = function query(options){
  33. return function query(req, res, next){
  34. if (!req.query) {
  35. req.query = ~req.url.indexOf('?')
  36. ? qs.parse(parse(req).query, options)
  37. : {};
  38. }
  39. next();
  40. };
  41. };