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 user4869326user4869326
Add a ment  | 

1 Answer 1

Reset to default 7

You 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