From 82601d8e84bdbadec8e736c2acf01c6b58f112c4 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Sun, 13 Jul 2014 23:56:37 -0400 Subject: [PATCH] Change req.query to a getter --- History.md | 2 ++ lib/application.js | 2 -- lib/express.js | 2 +- lib/middleware/query.js | 30 ------------------------------ lib/request.js | 23 +++++++++++++++++++++++ lib/utils.js | 12 ------------ 6 files changed, 26 insertions(+), 45 deletions(-) delete mode 100644 lib/middleware/query.js diff --git a/History.md b/History.md index 7a9889779a..2ba05b414c 100644 --- a/History.md +++ b/History.md @@ -5,8 +5,10 @@ - `res.json(obj, status)` signature - use `res.json(status, obj)` - `res.jsonp(obj, status)` signature - use `res.jsonp(status, obj)` - `res.send(body, status)` signature - use `res.send(status, body)` + - `express.query` middleware * change: - `req.host` now returns host (`hostname:port`) - use `req.hostname` for only hostname + - `req.query` is now a getter instead of a plain property 4.7.0 / 2014-07-25 ================== diff --git a/lib/application.js b/lib/application.js index c82126f4bb..4cc42df380 100644 --- a/lib/application.js +++ b/lib/application.js @@ -7,7 +7,6 @@ var mixin = require('utils-merge'); var Router = require('./router'); var methods = require('methods'); var middleware = require('./middleware/init'); -var query = require('./middleware/query'); var debug = require('debug')('express:application'); var View = require('./view'); var http = require('http'); @@ -100,7 +99,6 @@ app.lazyrouter = function() { strict: this.enabled('strict routing') }); - this._router.use(query(this.get('query parser fn'))); this._router.use(middleware.init(this)); } }; diff --git a/lib/express.js b/lib/express.js index 7be6832fb6..9f19efa362 100644 --- a/lib/express.js +++ b/lib/express.js @@ -56,7 +56,6 @@ exports.Router = Router; * Expose middleware */ -exports.query = require('./middleware/query'); exports.static = require('serve-static'); /** @@ -83,6 +82,7 @@ exports.static = require('serve-static'); 'limit', 'multipart', 'staticCache', + 'query', ].forEach(function (name) { Object.defineProperty(exports, name, { get: function () { diff --git a/lib/middleware/query.js b/lib/middleware/query.js deleted file mode 100644 index 092bbd9985..0000000000 --- a/lib/middleware/query.js +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Module dependencies. - */ - -var parseUrl = require('parseurl'); -var qs = require('qs'); - -/** - * @param {Object} options - * @return {Function} - * @api public - */ - -module.exports = function query(options) { - var queryparse = qs.parse; - - if (typeof options === 'function') { - queryparse = options; - options = undefined; - } - - return function query(req, res, next){ - if (!req.query) { - var val = parseUrl(req).query; - req.query = queryparse(val, options); - } - - next(); - }; -}; diff --git a/lib/request.js b/lib/request.js index 8f1cd8651b..c014f3798e 100644 --- a/lib/request.js +++ b/lib/request.js @@ -182,6 +182,29 @@ req.range = function(size){ return parseRange(size, range); }; +/** + * Parse the query string of `req.url`. + * + * This uses the "query parser" setting to parse the raw + * string into an object. + * + * @return {String} + * @api public + */ + +defineGetter(req, 'query', function query(){ + var queryparse = this.app.get('query parser fn'); + + if (!queryparse) { + // parsing is disabled + return Object.create(null); + } + + var querystring = parse(this).query; + + return queryparse(querystring); +}); + /** * Return the value of param `name` when present or `defaultValue`. * diff --git a/lib/utils.js b/lib/utils.js index 9f9fdfd6d0..bb5f06918e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -224,7 +224,6 @@ exports.compileQueryParser = function compileQueryParser(val) { fn = querystring.parse; break; case false: - fn = newObject; break; case 'extended': fn = qs.parse; @@ -289,14 +288,3 @@ exports.setCharset = function(type, charset){ // format type return typer.format(parsed); }; - -/** - * Return new empty objet. - * - * @return {Object} - * @api private - */ - -function newObject() { - return {}; -}