The querystring. parse method in node. js is described in node. jsurl. parse.

Source: Internet
Author: User

The querystring. parse method in node. js is described in node. jsurl. parse.

Method description:

Converts a string to an object. To put it bluntly, the parameter string on the url is converted into an array object. (See the example)

Syntax:

Copy codeThe Code is as follows:
Querystring. parse (str, [sep], [eq], [options])

Receiving parameters:

Str string to be converted

Set the separator for sep. The default value is '&'

The eq value assignment operator. The default value is '='

[Options] maximum length of a maxKeys acceptable string, 1000 by default

Example:

Copy codeThe Code is as follows:
Querystring. parse ('foo = bar & baz = qux & baz = quux & corge ')
// Returns
{Foo: 'bar', baz: ['qux', 'quux '], corge :''}

Source code:

Copy codeThe Code is as follows:

// Parse a key = val string.
QueryString. parse = QueryString. decode = function (qs, sep, eq, options ){
Sep = sep | '&';
Eq = eq | '= ';
Var obj = {};
If (! Util. isString (qs) | qs. length = 0 ){
Return obj;
}
Var regexp =/\ +/g;
Qs = qs. split (sep );
Var maxKeys = 1000;
If (options & util. isNumber (options. maxKeys )){
MaxKeys = options. maxKeys;
}
Var len = qs. length;
// MaxKeys <= 0 means that we shocould not limit keys count
If (maxKeys> 0 & len> maxKeys ){
Len = maxKeys;
}
For (var I = 0; I <len; ++ I ){
Var x = qs [I]. replace (regexp, '% 20 '),
Idx = x. indexOf (eq ),
Kstr, vstr, k, v;
If (idx> = 0 ){
Kstr = x. substr (0, idx );
Vstr = x. substr (idx + 1 );
} Else {
Kstr = x;
Vstr = '';
}
Try {
K = decodeURIComponent (kstr );
V = decodeURIComponent (vstr );
} Catch (e ){
K = QueryString. unescape (kstr, true );
V = QueryString. unescape (vstr, true );
}
If (! HasOwnProperty (obj, k )){
Obj [k] = v;
} Else if (util. isArray (obj [k]) {
Obj [k]. push (v );
} Else {
Obj [k] = [obj [k], v];
}
}
Return obj;
};

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.