美文网首页
nodejs url模块详解

nodejs url模块详解

作者: 他爱在黑暗中漫游 | 来源:发表于2018-05-17 00:27 被阅读18次

nodejs url模块

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

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

  • urlString <string> 要解析的 URL 字符串。
  • parseQueryString <boolean> 如果为 true,则 query 属性总会通过 querystring 模块的 parse() 方法生成一个对象。 如果为 false,则返回的 URL 对象上的 query 属性会是一个未解析、未解码的字符串。 默认为 false
  • slashesDenoteHost <boolean> 如果为 true,则 // 之后至下一个 / 之前的字符串会被解析作为 host。 例如,//foo/bar 会被解析为 {host: 'foo', pathname: '/bar'} 而不是 {pathname: '//foo/bar'}。 默认为 false
    url.parse() 方法会解析一个 URL 字符串并返回一个 URL 对象。

如果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)

  • urlObject <Object> | <string> 一个 URL 对象(就像 url.parse() 返回的)。 如果是一个字符串,则通过 url.parse() 转换为一个对象。

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)

  • from <string> 解析时相对的基本 URL。
  • to <string> 要解析的超链接 URL。

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

相关文章

  • nodejs url模块详解

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

  • nodejs url模块详解

    个人原创,如需转载,请联系作者 URL模块用于解析和处理URL字符串,提供了三个方法: parse format ...

  • nodejs 中有哪些常用的内置模块

    path模块nodejs中的path模块用于处理文件和目录的路径url模块在nodejs中url模块是用来解析ur...

  • nodejs静态资源服务器

    缩写含义 http是nodejs的服务模块 url是url路由模块 fs是文件服务器模块 nodejs服务器的创建...

  • nodejs静态资源服务器

    nodejs静态资源服务器 1、http 是nodejs的服务模块 2、url 是url路由模块 3、fs 是文件...

  • node js调试相关(ts-node)

    一、debug模块 参考npm debug模块开始学nodejs —— 调试篇debug模块详解一般在nodejs...

  • Node中url模块的方法

    URL模块是NodeJS的核心模块之一,用于解析url字符串和url对象 1、url.parse(url_str[...

  • Node url模块

    url Nodejs的url模块只要提供一些实用函数,用于URL的处理和解析。在Nodejs中可以直接引用url模...

  • 笔记 第六天 nodejs模块

    nodejs模块 nodejs 的文件操作 nodejs的io键盘交互 nodejs的url判断渲染模板 node...

  • day6-课堂笔记

    本节课内容: NodeJS的模块 NodeJS的文件操作 NodeJS的io键盘交互 NodeJs的url判断渲染...

网友评论

      本文标题:nodejs url模块详解

      本文链接:https://www.haomeiwen.com/subject/kmrvdftx.html