用到,做个笔记
querystring模块提供了有关查询字符串参数处理的操作,提供具体以下4个方法:
querystring.stringify(obj, [sep], [eq])
querystring.parse(str, [sep], [eq], [options])
querystring.escape
querystring.unescape
querystring.stringify(obj, [sep], [eq])
将一个对象序列化成字符串请求参数,参数obj指要序列化的对象
[sep]
:分隔符,默认值&
[eq]
:分配符,默认值=
var querystring = require('querystring'); var obj = {username:'zheng', password:'123456'}; console.log(querystring.stringify(obj));
输出结果为:
username=zheng&password=123456
querystring.parse(str, [sep], [eq], [options])
将字符串序解析成对象
[sep]
:分隔符,默认值&
[eq]
:分配符,默认值=
[options]
:限制对象中键值对的数量,默认值1000
,设置为0
则不限制var str1 = 'username=zheng&password=123456'; var str2 = 'username=zheng&password=123456&tag=movie&tag=music'; console.log(querystring.parse(str1)); console.log(querystring.parse(str2));
输出结果为:
{ username: ‘zheng’, password: ‘123456’ }
{ username: ‘zheng’, password: ‘123456’, tag: [ ‘movie’, ‘music’ ] }querystring.escape
对参数中的字符串进行编码
querystring.unescape
对参数进行解码
var str = 'username=用户名&password=123456'; var escape = querystring.escape(str); var unescape = querystring.unescape(escape); console.log(escape); console.log(unescape);
输出结果为:
username%3D%E7%94%A8%E6%88%B7%E5%90%8D%26password%3D123456
‘username=用户名&password=123456’