nodejs url模块详解

2018-05-17  本文已影响18人  他爱在黑暗中漫游

nodejs url模块

nodejs中用户url格式化和反格式化模块
用于url解析、处理等操作的解决方案

1.url.parse(urlString[, parseQueryString[, slashesDenoteHost]])

如果urlString不是字符串将会抛出TypeError。

如果auth属性存在但无法编码则抛出URIError。

示例1:

var url = require("url")
var myurl="http://www.nodejs.org/some/url/?with=query&param=that#about"
parsedUrl=url.parse(myurl)

结果

{ protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'www.nodejs.org',
  port: null,
  hostname: 'www.nodejs.org',
  hash: '#about',
  search: '?with=query&param=that',
  query: 'with=query&param=that',
  pathname: '/some/url/',
  path: '/some/url/?with=query&param=that',
  href: 'http://www.nodejs.org/some/url/?with=query&param=that#about' 
}

当parse方法第二个参数为true时,结果如下

parsedUrl=url.parse(myurl,true)
{ protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'www.nodejs.org',
  port: null,
  hostname: 'www.nodejs.org',
  hash: '#about',
  search: '?with=query&param=that',
  query: { with: 'query', param: 'that' },
  pathname: '/some/url/',
  path: '/some/url/?with=query&param=that',
  href: 'http://www.nodejs.org/some/url/?with=query&param=that#about' }

2.url.format(urlObject)

url.format() 方法返回一个从 urlObject 格式化后的 URL 字符串。

如果 urlObject 不是一个对象或字符串,则 url.format() 抛出 TypeError

示例

var url=require('url');  
var obj1={ protocol: 'http:',      
  slashes: true,         
  auth: null,           
  host: 'calc.gongjuji.net',   
  port: null,                 
  hostname: 'calc.gongjuji.net',  
  hash: '#one#two',              
  search: '?name=zhangsan&age=18',  
  query: 'name=zhangsan&age=18',    
  pathname: '/byte/',              
  path: '/byte/?name=zhangsan&age=18',  
  href: 'http://calc.gongjuji.net/byte/?name=zhangsan&age=18#one#two'   
};  
var url1=url.format(obj1);  
console.log(url1);//http://calc.gongjuji.net/byte/?name=zhangsan&age=18#one#two  
//请求参数为为json对象  
var obj2={ protocol: 'http:',  
slashes: true,  
auth: null,  
host: 'calc.gongjuji.net',  
port: null,  
hostname: 'calc.gongjuji.net',  
hash: '#one#two',  
search: '?name=zhangsan&age=18',  
query: { name: 'zhangsan', age: '18' }, //页面参数部分,已经解析成对象了  
pathname: '/byte/',  
path: '/byte/?name=zhangsan&age=18',  
href: 'http://calc.gongjuji.net/byte/?name=zhangsan&age=18#one#two' };  
var url2=url.format(obj2);  
console.log(url2); //http://calc.gongjuji.net/byte/?name=zhangsan&age=18#one#two  
//缺少参数的情况  
var obj3={ protocol: null,  
slashes: true,  
auth: null,  
host: 'www.gongjuji.net',  
port: null,  
hostname: 'www.gongjuji.net',  
hash: '#one',  
search: '?name=zhangsan',  
query: { name: 'zhangsan' },  
pathname: '/byte/',  
path: '/byte/?name=zhangsan',  
href: '//www.gongjuji.net/byte/?name=zhangsan#one' };  
var url3=url.format(obj3);  
console.log(url3);//www.gongjuji.net/byte/?name=zhangsan#one  

3.url.resolve(from, to)

url.resolve() 方法会以一种 Web 浏览器解析超链接的方式把一个目标 URL 解析成相对于一个基础 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'

参考:
https://www.jianshu.com/p/fb5278d02cc4
http://nodejs.cn/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost
https://blog.csdn.net/u011127019/article/details/52350172

上一篇 下一篇

猜你喜欢

热点阅读