admin管理员组文章数量:1335832
app.js
var bodyParser = require('koa-bodyparser');
app.use(bodyParser());
app.use(route.get('/objects/', objects.all));
objects.js
module.exports.all = function * all(next) {
this.body = yield objects.find({});
};
This works fine for get all objects. But I wanna get by query param, something like this localhost:3000/objects?city=Toronto How to use "city=Toronto" in my objects.js?
app.js
var bodyParser = require('koa-bodyparser');
app.use(bodyParser());
app.use(route.get('/objects/', objects.all));
objects.js
module.exports.all = function * all(next) {
this.body = yield objects.find({});
};
This works fine for get all objects. But I wanna get by query param, something like this localhost:3000/objects?city=Toronto How to use "city=Toronto" in my objects.js?
Share Improve this question edited Jan 12, 2017 at 17:06 asked Jan 12, 2017 at 9:01 user4869326user48693261 Answer
Reset to default 7You can use this.query
to access all your query parameters.
For example, if the request came at the url /objects?city=Toronto&color=green
you would get the following:
function * routeHandler (next) {
console.log(this.query.city) // 'Toronto'
console.log(this.query.color) // 'green'
}
If you want access to the entire querystring, you can use this.querystring
instead. You can read more about it in the docs.
Edit: With Koa v2, you would use ctx.query
instead of this.query
.
本文标签: javascriptHow to get query string in koa with koabodyparserStack Overflow
版权声明:本文标题:javascript - How to get query string in koa with koa-bodyparser? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742387706a2465366.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论