koa-qs
qs for koa
Last updated 11 years ago by fengmk2 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install koa-qs 
SYNC missed versions from official npm registry.

Koa Querystring

NPM version build status Test coverage David deps node version npm download

By default, Koa uses the native querystring module which does not provide nesting support. This patches a koa app with nesting support via the qs support, which is also used by Connect and Express.

Simply wrap a koa app with this module:

// Koa 1.x.x
const koa = require('koa')
const app = koa()
require('koa-qs')(app)
// Koa 2.x.x
const Koa = require('koa')
const app = new Koa()
require('koa-qs')(app)

Optional parse mode

There're three parse mode.

extended mode

The default mode, use [qs] module.

require('koa-qs')(app, 'extended')

simple mode

Use querystring module, same as koa does by default. If you want to use this mode, don't use this module.

strict mode

This mode make this.query.foo return strict array.

require('koa-qs')(app, 'strict')

What's different

A normal request GET /foo?p=a&q=foo&q=bar.

  • before patch
console.log('%j', this.query);
{
  "p": "a",
  "q": ["foo", "bar"]
}
  • after patch
console.log('%j', this.query);
{
  "p": ["a"],
  "q": ["foo", "bar"]
}

first mode

This mode make this.query.foo return strict string. Disable multi values.

If querystring contains multi same name params, return the first item.

require('koa-qs')(app, 'first')

In 95% use cases, application only want string query params.

This patch can avoid some stupid TypeError and some security issues like MongoDB inject when the developers forget handling query params type check.

What's different

A normal request GET /foo?p=a,b&p=b,c.

  • before patch
console.log('%j', this.query.p);
["a,b", "b,c"]
  • after patch
console.log('%j', this.query.p);
"a,b"

License

MIT

Current Tags

  • 3.0.0                                ...           latest (6 years ago)

5 Versions

  • 3.0.0                                ...           6 years ago
  • 2.0.0                                ...           11 years ago
  • 1.1.0                                ...           11 years ago
  • 1.0.1                                ...           12 years ago
  • 1.0.0                                ...           12 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (2)
Dev Dependencies (6)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |