Description of Querystring.parse method used in Node.js _node.js

Source: Internet
Author: User

Method Description:

Turns the string into an object. In fact, the URL with the parameter string to the array of objects. (see examples to know)

Grammar:

Copy Code code as follows:

Querystring.parse (str, [Sep], [eq], [options])

Receive parameters:

STR wants to convert string

Sep set Separator, default to ' & '

EQ set assignment, default to ' = '

[Options] Maxkeys can accept the maximum length of a string, default to 1000

Example:

Copy Code code as follows:

Querystring.parse (' Foo=bar&baz=qux&baz=quux&corge ')
Returns
{foo: ' Bar ', Baz: [' Qux ', ' Quux '], Corge: '}

Source:

Copy Code code 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 should 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;
};

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.