Node.js学习之地址解析模块URL的使用详解

所属分类: 网络编程 / JavaScript 阅读数: 1090
收藏 0 赞 0 分享

前言

本文主要给大家介绍了关于Node.js地址解析模块URL使用的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

url结构化/模块化/路径解析

  • 结构化:url.parse(urlString[, parseQueryString[, slashesDenoteHost]])
  • 模块化:url.format(urlObject)
  • 路径解析:url.resolve(from, to)

一个URL字符串是一个结构化的字符串包含多个有意义的组件。在解析时,返回一个URL对象包含每一个组件的属性。

官方手册上面的一张图是这样子的:

这张图解释了一个url结构化成哪些部分,哪些部分又包含哪些部分

protocol: 请求协议

host: URL主机名已全部转换成小写, 包括端口信息

auth:URL中身份验证信息部分

hostname:主机的主机名部分, 已转换成小写

port: 主机的端口号部分

pathname: URL的路径部分,位于主机名之后请求查询之前

search: URL 的“查询字符串”部分,包括开头的问号。

path: pathname 和 search 连在一起。

query: 查询字符串中的参数部分(问号后面部分字符串),或者使用 querystring.parse() 解析后返回的对象。

  hash: URL 的 “#” 后面部分(包括 # 符号)

url结构化

将一个url地址结构化成为拥有上图属性的url对象。url.parse第二个和第三个参数默认为false。

  • 第二个参数决定query属性值是字符串还是对象
  • 第三个参数如果为true,//后的第一个令牌文字字符串和下一个/之间的文字字符串将被解释为主机

例子如下

const url = require("url");
var urlstr = "http://localhost:8888/bb?name=bigbear&memo=helloworld&memo=helloC";
var urlobj = url.parse(urlstr); 
console.log(urlobj);
/*
Url {
 protocol: 'http:',
 slashes: true,
 auth: null,
 host: 'localhost:8888',
 port: '8888',
 hostname: 'localhost',
 hash: null,
 search: '?name=bigbear&memo=helloworld&memo=helloC',
 query: 'name=bigbear&memo=helloworld&memo=helloC',
 pathname: '/bb',
 path: '/bb?name=bigbear&memo=helloworld&memo=helloC',
 href: 'http://localhost:8888/bb?name=bigbear&memo=helloworld&memo=helloC' }
*/

第二个参数为true时

query: { name: ‘bigbear', memo: [ ‘helloworld', ‘helloC' ] },

例子如下:

const url = require("url");
var urlstr = "http://localhost:8888/bb?name=bigbear&memo=helloworld&memo=helloC";
console.log(
 url.parse(urlstr, true)
)
/*
Url {
 protocol: 'http:',
 slashes: true,
 auth: null,
 host: 'localhost:8888',
 port: '8888',
 hostname: 'localhost',
 hash: null,
 search: '?name=bigbear&memo=helloworld&memo=helloC',
 query: { name: 'bigbear', memo: [ 'helloworld', 'helloC' ] },
 pathname: '/bb',
 path: '/bb?name=bigbear&memo=helloworld&memo=helloC',
 href: 'http://localhost:8888/bb?name=bigbear&memo=helloworld&memo=helloC' }
*/

第三个参数对比

例子如下:

const url = require("url");
var urlstr = "//foo/bar ";
console.log(
 url.parse(urlstr, true,true)
)
/*
 输出:Url {
 protocol: null,
 slashes: true,
 auth: null,
 host: 'foo',
 port: null,
 hostname: 'foo',
 hash: null,
 search: '',
 query: {},
 pathname: '/bar',
 path: '/bar',
 href: '//foo/bar' }
*/


const url = require("url");
var urlstr = "//foo/bar ";
console.log(
 url.parse(urlstr)
)
/*
输出:
Url {
 protocol: null,
 slashes: null,
 auth: null,
 host: null,
 port: null,
 hostname: null,
 hash: null,
 search: null,
 query: null,
 pathname: '//foo/bar',
 path: '//foo/bar',
 href: '//foo/bar' }
*/

url模块化

将一个url对象转换成一个url字符串,url对象中的属性为url.parse()产生的对象的属性。

url.parse()url.format()互为逆操作。

例子如下:

const url = require("url");
var Urlobj = {
 protocol: 'http:',
 slashes: true,
 auth: null,
 host: 'localhost:8888',
 port: '8888',
 hostname: 'localhost',
 hash: null,
 search: '?name=bigbear&memo=helloworld&memo=helloC',
 query: { name: 'bigbear', memo: [ 'helloworld', 'helloC' ] },
 pathname: '/bb',
 path: '/bb?name=bigbear&memo=helloworld&memo=helloC',
 }
console.log(
 url.format(Urlobj)
)
//输出:http://localhost:8888/bb?name=bigbear&memo=helloworld&memo=helloC

路径解析:url.resolve(from, to)

url.resolve()方法解决了目标URL相对于基本URL的方式类似于Web浏览器解决锚标记href。

官方手册例子:

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'

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

更多精彩内容其他人还在看

jQuery 行级解析读取XML文件(附源码)

项目中应用jQuery解析读取XML语言配置文件来实现语言的调度。这是jQuery解析读取XML文件功能的测试源码,现拿出来分享。
收藏 0 赞 0 分享

JS 文件本身编码转换 图文教程

JS编码转换,这句话本身就是一句具有二重义的话。通常理解为JS文件里能转换编码的代码,但是,我所碰到的问题并不是这样的,是要解决JS文件本身的编码问题,它是UTF-8编码的还是ANSI编码的?
收藏 0 赞 0 分享

jQuery Ajax之$.get()方法和$.post()方法

load()方法通常用来从Web服务器上获取静态的数据文件,然而这并不能体现Ajax的全部价值。在项目中,如果需要传递一些参数给服务器中的页面,那么可以使用$.get()或者$.post()方法(或者是后面要讲解到的$.ajax方法)。
收藏 0 赞 0 分享

jQuery Ajax之load()方法

jQuery对Ajax操作进行了封装,在jQuery中$.ajax()方法属于最底层的方法,第2层是laod()、$.get()和$.post()方法,第3层是$.getScript()和$.getJSON()方法。
收藏 0 赞 0 分享

JavaScript 核心参考教程 内置对象

JavaScript 是根据 "ECMAScript"标准制定的网页脚本语言。这个标准由 ECMA 组织发展和维护。ECMA-262 是正式的 JavaScript 标准。
收藏 0 赞 0 分享

JavaScript 核心参考教程 RegExp对象

JavaScript 核心参考教程RegExp对象,学习正则表达式的朋友可以参考下。
收藏 0 赞 0 分享

javascript hashtable实现代码

javascript中没有像c#,java那样的哈希表(hashtable), 然而,javascript中的Array也只有一些类似于'哈希表'的非常简单功能。
收藏 0 赞 0 分享

百度留言本js 大家可以参考下

百度留言本js 大家可以参考下。
收藏 0 赞 0 分享

javascript 判断某年某月有多少天的实现代码 推荐

以前写网页的时候,经常碰到选择日期的问题,其实就是判断某年某月有多少天。
收藏 0 赞 0 分享

让iframe子窗体取父窗体地址栏参数(querystring)

突然用到,记录一下,对地址栏字符串用正则处理最好,有时间研究一下。 主要是思路。
收藏 0 赞 0 分享
查看更多