3.nodejs(三) 常用API

querystring: --- > qs npm i qs ==> qs:parse/stringify只有一个参数

JSON.parse  字符串转对象
JSON.stringify 对象转字符串

qs.parse() --- decode
qs.stringify() --- encode

parse/stringify(str/json,第一次切割符号,第二次切割符号)

qs.escape()   编码   encodeURIComponent
qs.unescape()  解码   decodeURIComponent

1.qs.parse(); qs.stringify(); qs.js var qs = require("querystring"); var str = "a=12&b=5" //{a:12,b:5} var json = qs.parse(str); console.log(json); console.log(qs.stringify(json));

res:

my@my-THINK MINGW64 /d/workspace/7.11 $ node qs { a: '12', b: '5' } a=12&b=5 qs3.js

//qs.parse/stringify(str,第一次切割的符号,第二次切割的符号)

var qs = require("querystring"); var str = "a|12=b|5=age|20"; //qs.parse/stringify(str,第一次切割的符号,第二次切割的符号) var json = qs.parse(str,"=","|"); console.log(json); console.log(qs.stringify(json,"=","|"));

res

my@my-THINK MINGW64 /d/workspace/7.11 $ node qs3 { a: '12', b: '5', age: '20' } a|12=b|5=age|20 qs4.js

引入第三方模块 npm i qs

//qs.parse/stringify(str)
后面不能跟分隔符号

var qs = require("qs"); var str = "a=12&b=5"; var json = qs.parse(str); console.log(json); console.log((qs.stringify(json)));

res:

my@my-THINK MINGW64 /d/workspace/7.11 $ node qs4 { a: '12', b: '5' } a=12&b=5 2.qs.escape();编码 qs.unescape();解码 qs2.js var qs = require("querystring"); var str = "aaa中文"; var code = encodeURIComponent(str); console.log(code); console.log(decodeURIComponent(code)); console.log("----------------------"); var code = qs.escape(str); console.log(code); console.log(qs.unescape(code));

res:

my@my-THINK MINGW64 /d/workspace/7.11 $ node qs2 aaa%E4%B8%AD%E6%96%87 aaa中文 ---------------------- aaa%E4%B8%AD%E6%96%87 aaa中文 3. qs.encode()//转字符串 qs.decode()//转对象 parse qs5.js var qs = require("querystring"); //encode|decode //var str = "aaa中文"; var str = "a=12&b=5"; var str = {a:12,b:5}; var code = qs.encode(str);//转字符串 stringify console.log(code); console.log(qs.decode(code));//转对象 parse

res:

my@my-THINK MINGW64 /d/workspace/7.11 $ node qs5 a=12&b=5 { a: '12', b: '5' } 2.url

url:
url.parse()--->urlObj
url.format(urlObj)--> strUrl
url.resolve(from, to);

URLSearchParams new URLSearchParams();

地址组成: :80/index.html?user=aaa&pass=123#page6 :80 /index.html ?user=aaa&pass=123 #page6 协议 域名 端口 路径(资源地址) 数据 锚点 哈希hash location Url { protocol: 'http:', slashes: true, auth: null, host: 'www.baidu.com:80', port: '80', hostname: 'www.baidu.com', hash: '#page6', search: '?user=aaa&pass=123', query: 'user=aaa&pass=123', pathname: '/index.html', path: '/index.html?user=aaa&pass=123', href: 'http://www.baidu.com:80/index.html?user=aaa&pass=123#p age6' } URL { href: 'http://www.baidu.com/index.html?user=aaa&pass=123#page6', origin: 'http://www.baidu.com', protocol: 'http:', username: '', password: '', host: 'www.baidu.com', hostname: 'www.baidu.com', port: '', pathname: '/index.html', search: '?user=aaa&pass=123', searchParams: URLSearchParams { 'user' => 'aaa', 'pass' => '123' }, hash: '#page6' } 1. .parse()转成对象 .format()转成字符串 url1.js var modUrl = require("url"); var url = "http://www.baidu.com:80/index.html?user=aaa&pass=123#page6"; var urlObj = modUrl.parse(url); console.log(1,urlObj); console.log(2,modUrl.format(urlObj));

res:

my@my-THINK MINGW64 /d/workspace/7.11 $ node url1 1 Url { protocol: 'http:', slashes: true, auth: null, host: 'www.baidu.com:80', port: '80', hostname: 'www.baidu.com', hash: '#page6', search: '?user=aaa&pass=123', query: 'user=aaa&pass=123', pathname: '/index.html', path: '/index.html?user=aaa&pass=123', href: 'http://www.baidu.com:80/index.html?user=aaa&pass=123#page6' } 2 'http://www.baidu.com:80/index.html?user=aaa&pass=123#page6' url2.js var modUrl = require("url"); var url = "http://www.baidu.com:9000/index.html?user=aaa&pass=123#page6"; console.log(1,new modUrl.Url(url)); console.log(2,new modUrl.URL(url));

res:

my@my-THINK MINGW64 /d/workspace/7.11 $ node url2 1 Url { protocol: null, slashes: null, auth: null, host: null, port: null, hostname: null, hash: null, search: null, query: null, pathname: null, path: null, href: null } 2 URL { href: 'http://www.baidu.com:9000/index.html?user=aaa&pass=123#page6', origin: 'http://www.baidu.com:9000', protocol: 'http:', username: '', password: '', host: 'www.baidu.com:9000', hostname: 'www.baidu.com', port: '9000', pathname: '/index.html', search: '?user=aaa&pass=123', searchParams: URLSearchParams { 'user' => 'aaa', 'pass' => '123' }, hash: '#page6' } url3.js

.resolve(),替换路径

var modUrl = require("url"); console.log(modUrl.resolve('http://localhost:9000/login', 'index')); console.log(modUrl.resolve('http://localhost:9000/login', '/index')); console.log(modUrl.resolve('http://localhost:9000/users/login', 'index')); //不加/只替换最后一个相对路径 console.log(modUrl.resolve('http://localhost:9000/users/login', '/index')); //加/替换绝对路径

res:

my@my-THINK MINGW64 /d/workspace/7.11 $ node url3 :9000/index :9000/index :9000/users/index :9000/index url4.js

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpyyfz.html