NodeJS API
URL
url.parse(urlString[,parseQueryString[,slashesDenoteHost]])
参数说明:
urlString 转换的url
parseQueryString如果为true,query属性会变成对象,默认为false
slashesDenoteHost如果为true,默认为false ,考虑以下例子 //for/bar,会被转换成
{host:'foo',pathname:'/bar'}
而不是
{pathname:'//foo/bar'
let url =require('url')
let query_url= url.parse('http://www/baidu.com/query?id=123')
let url_str=url.parse('//foo/bar',true,true)
console.log(url_str)
console.log(query_url)
url.format(URL[,options])
返回可自定义序列化的URL string
let url = require('url')
const myURL= new URL('https://www.bing.com/test#/demo?query=1')
let url_obj=url.format(myURL)
console.log(url_obj)
url.resolve(from,to)
以浏览器解析 href 方式解析从基本路径到目录路径的URL
const url = require('url')
url.resolve('/one/two/three', 'four'); // '/one/two/four'
url.resolve('http://example.com/', '/one'); // 'http://example.com/one'
url.resolve('http://example.com/one', '/two'); // 'http://example.com/two'
querystring 查询字符串
querystring.escape(str)
百分比编码
let querystring = require('querystring')
let str = querystring.escape('百分比编码')
querystring.parse(str[,sep[,eq[,options]]])
解析URL查询字符串
let querystring = require('querystring')
let str = querystring.parse('w=%D6%D0%CE%C4&foo=bar')
console.log(str.w);
let querystring = require("querystring");
let str = querystring.parse("foo=bar&abc=xyz&abc=123")
console.log(str);
// [Object: null prototype] { foo: 'bar', abc: [ 'xyz', '123' ] }
querystring.stringify(obj[,sep[,eq[,options]]])
序列化为URL查询字符串的对象
querystring.stringify({foo:'bar',baz:['qux','quux'],corge:''})
querystring.unescape(str)
在给定的 str 上执行 URL 百分比编码字符的解码
let querystring = require("querystring");
let str = querystring.unescape("%E7%99%BE%E5%88%86%E6%AF%94%E7%BC%96%E7%A0%81")
console.log(str); //百分比编码