Skip to content

Commit 68ebe75

Browse files
url
1 parent 4585832 commit 68ebe75

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

Network/url.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
## url
2+
3+
url 规范仅允许 英文字母、数字和一些保留字符存在,其他字符均需要编码
4+
编码规则很简单, 使用 %+一个字节的十六进制,
5+
如:
6+
7+
* ` `(空格),十六进制为 20,编码为 %20
8+
* ``, 十六进制为 E59388,编码为 %E5%93%88
9+
10+
```js
11+
const cn = ''; // %E5%93%88
12+
console.log(Buffer.from(cn).toString('hex')); // e59388
13+
14+
const space =' ';
15+
console.log(Buffer.from(space).toString('hex')); // 20
16+
const url = `http://localhost:3000/test?title=${cn}&a=b&c=d${space}`;
17+
const enUrl = encodeURI(url);
18+
const deUrl = decodeURI(enUrl);
19+
20+
console.log(enUrl); // http://localhost:3000/test?title=%E5%93%88&a=b&c=d%20,
21+
console.log(deUrl); // http://localhost:3000/test?title=哈&a=b&c=d
22+
```
23+
24+
### querystring vs qs
25+
url 格式中的 querystring 解析
26+
27+
node.js querystring
28+
29+
```js
30+
const querystring = require('querystring');
31+
const qs = require('qs');
32+
33+
34+
// stringify
35+
const obj1 = {a:'a', b:['b', 'b'], c:''};
36+
const obj2 = {a:'a', b:['b', 'b', {c:'c'}], d:{e:'e'}};
37+
const str1 = `a=a&b=b&b=b&c=%E5%93%88`; // a=a&b=b&b=b&c=哈
38+
const str2 = `a=a&b%5B0%5D=b&b%5B1%5D=b&c=%E5%93%88`; // a=a&b[0]=b&b[1]=b&c=哈
39+
const str3 = `a=a&b%5B0%5D=b&b%5B1%5D=b&b%5B2%5D%5Bc%5D=c&d%5Be%5D=e`; // a=a&b[0]=b&b[1]=b&b[2][c]=c&d[e]=e
40+
41+
42+
const qStr1 = querystring.stringify(obj1);
43+
console.log(qStr1); // a=a&b=b&b=b&c=%E5%93%88
44+
const qStr2 = qs.stringify(obj1);
45+
console.log(qStr2); // a=a&b%5B0%5D=b&b%5B1%5D=b&c=%E5%93%88 // a=a&b[0]=b&b[1]=b&c=哈
46+
47+
48+
const qStr3 = querystring.stringify(obj2);
49+
console.log(qStr3); //a=a&b=b&b=b&b=&d= // 嵌套对象有问题
50+
const qStr4 = qs.stringify(obj2);
51+
console.log(qStr4); // a=a&b%5B0%5D=b&b%5B1%5D=b&b%5B2%5D%5Bc%5D=c&d%5Be%5D=e // a=a&b[0]=b&b[1]=b&b[2][c]=c&d[e]=e
52+
53+
54+
55+
// parse querystring 格式
56+
const qObj1 = querystring.parse(str1);
57+
console.log(qObj1); // { a: 'a', b: [ 'b', 'b' ], c: '哈' }
58+
const qObj2 = qs.parse(str1);
59+
console.log(qObj2); // { a: 'a', b: [ 'b', 'b' ], c: '哈' }
60+
61+
// parse qs 格式
62+
const qObj3 = querystring.parse(str2);
63+
console.log(qObj3); // { a: 'a', 'b[0]': 'b', 'b[1]': 'b', c: '哈' }, // querystring 不支持 qs 的对象格式
64+
const qObj4 = qs.parse(str2);
65+
console.log(qObj4); // { a: 'a', b: [ 'b', 'b' ], c: '哈' }, // qs 可以自己还原自己的格式
66+
67+
// parse qs 复杂对象格式
68+
const qObj5 = querystring.parse(str3);
69+
console.log(qObj5); // { a: 'a', 'b[0]': 'b', 'b[1]': 'b', 'b[2][c]': 'c', 'd[e]': 'e' }
70+
const qObj6 = qs.parse(str3);
71+
console.log(qObj6); // { a: 'a', b: [ 'b', 'b', { c: 'c' } ], d: { e: 'e' } }
72+
73+
``

Network/url/index.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
// const cn = '哈'; // %E5%93%88
3+
// console.log(Buffer.from(cn).toString('hex')); // e59388
4+
//
5+
// const space =' ';
6+
// console.log(Buffer.from(space).toString('hex')); // 20
7+
// const url = `http://localhost:3000/test?title=${cn}&a=b&c=d${space}`;
8+
// const enUrl = encodeURI(url);
9+
// const deUrl = decodeURI(enUrl);
10+
//
11+
// console.log(enUrl); // http://localhost:3000/test?title=%E5%93%88&a=b&c=d%20,
12+
// console.log(deUrl); // http://localhost:3000/test?title=哈&a=b&c=d
13+
14+
15+
// const querystring = require('querystring');
16+
// const qs = require('qs');
17+
//
18+
//
19+
// // stringify
20+
// const obj1 = {a:'a', b:['b', 'b'], c:'哈'};
21+
// const obj2 = {a:'a', b:['b', 'b', {c:'c'}], d:{e:'e'}};
22+
// const str1 = `a=a&b=b&b=b&c=%E5%93%88`; // a=a&b=b&b=b&c=哈
23+
// const str2 = `a=a&b%5B0%5D=b&b%5B1%5D=b&c=%E5%93%88`; // a=a&b[0]=b&b[1]=b&c=哈
24+
// const str3 = `a=a&b%5B0%5D=b&b%5B1%5D=b&b%5B2%5D%5Bc%5D=c&d%5Be%5D=e`; // a=a&b[0]=b&b[1]=b&b[2][c]=c&d[e]=e
25+
//
26+
//
27+
// const qStr1 = querystring.stringify(obj1);
28+
// console.log(qStr1); // a=a&b=b&b=b&c=%E5%93%88
29+
// const qStr2 = qs.stringify(obj1);
30+
// console.log(qStr2); // a=a&b%5B0%5D=b&b%5B1%5D=b&c=%E5%93%88 // a=a&b[0]=b&b[1]=b&c=哈
31+
//
32+
//
33+
// const qStr3 = querystring.stringify(obj2);
34+
// console.log(qStr3); //a=a&b=b&b=b&b=&d= // 嵌套对象有问题
35+
// const qStr4 = qs.stringify(obj2);
36+
// console.log(qStr4); // a=a&b%5B0%5D=b&b%5B1%5D=b&b%5B2%5D%5Bc%5D=c&d%5Be%5D=e // a=a&b[0]=b&b[1]=b&b[2][c]=c&d[e]=e
37+
//
38+
//
39+
//
40+
// // parse querystring 格式
41+
// const qObj1 = querystring.parse(str1);
42+
// console.log(qObj1); // { a: 'a', b: [ 'b', 'b' ], c: '哈' }
43+
// const qObj2 = qs.parse(str1);
44+
// console.log(qObj2); // { a: 'a', b: [ 'b', 'b' ], c: '哈' }
45+
//
46+
// // parse qs 格式
47+
// const qObj3 = querystring.parse(str2);
48+
// console.log(qObj3); // { a: 'a', 'b[0]': 'b', 'b[1]': 'b', c: '哈' }, // querystring 不支持 qs 的对象格式
49+
// const qObj4 = qs.parse(str2);
50+
// console.log(qObj4); // { a: 'a', b: [ 'b', 'b' ], c: '哈' }, // qs 可以自己还原自己的格式
51+
//
52+
// // parse qs 复杂对象格式
53+
// const qObj5 = querystring.parse(str3);
54+
// console.log(qObj5); // { a: 'a', 'b[0]': 'b', 'b[1]': 'b', 'b[2][c]': 'c', 'd[e]': 'e' }
55+
// const qObj6 = qs.parse(str3);
56+
// console.log(qObj6); // { a: 'a', b: [ 'b', 'b', { c: 'c' } ], d: { e: 'e' } }
57+
58+

Network/url/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"qs": "^6.7.0"
4+
}
5+
}

0 commit comments

Comments
 (0)