express.query()是什么作用?
module.exports = function query(options) {
var opts = Object.create(options || null);
var queryparse = qs.parse;
if (typeof options === 'function') {
queryparse = options;
opts = undefined;
}
if (opts !== undefined && opts.allowPrototypes === undefined) {
// back-compat for qs module
opts.allowPrototypes = true;
}
return function query(req, res, next){
if (!req.query) {
var val = parseUrl(req).query;
req.query = queryparse(val, opts);
}
next();
};
};
上面的作用是用来解析url的吧,但是当没有使用express.query()这个中间件的时候,req.query得到的结果还是一样的。所以产生了疑惑~~
1 回复
//application.js
var query = require('./middleware/query');
app.lazyrouter = function lazyrouter() {
if (!this._router) {
this._router = new Router({
caseSensitive: this.enabled('case sensitive routing'),
strict: this.enabled('strict routing')
});
this._router.use(query(this.get('query parser fn')));
this._router.use(middleware.init(this));
}
};
看源码默认已加载此中间件