Code Monkey home page Code Monkey logo

qs's Introduction

Koa Querystring

NPM version build status Test coverage David deps iojs version 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:

var koa = require('koa')
var app = 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

qs's People

Contributors

dead-horse avatar fengmk2 avatar jonathanong avatar navaru avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.