美文网首页js学习
nodejs之url模块使用

nodejs之url模块使用

作者: 沈祥佑 | 来源:发表于2019-04-06 01:38 被阅读28次

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


url.png

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

  • protocol: 请求协议
  • host: URL主机名已全部转换成小写, 包括端口信息
  • auth:URL中身份验证信息部分
  • hostname:主机的主机名部分, 已转换成小写
  • port: 主机的端口号部分
  • pathname: URL的路径部分,位于主机名之后请求查询之前
  • search: URL 的“查询字符串”部分,包括开头的问号。
  • path: pathname 和 search 连在一起。
  • query: 查询字符串中的参数部分(问号后面部分字符串),或者使用 querystring.parse() 解析后返回的对象。
  • hash: URL 的 “#” 后面部分(包括 # 符号)

nodejs自带URL模块

url.parse()

url.parse(urlStr, [parseQueryString], [slashesDenoteHost])

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

参数使用说明如下:

  • urlStr - 需要接收的url字符串。
  • parseQueryString - 为true时将使用查询模块分析查询字符串,默认为false。
  • shashesDenoteHost
    默认为false,//foo/bar 形式的字符串将被解释成 { pathname: ‘//foo/bar' }
    如果设置成true,//foo/bar 形式的字符串将被解释成 { host: ‘foo', pathname: ‘/bar' }

例如:

const url = require("url");
var urlstr = "http://localhost:8888/a?n=big&m=he&m=cc#hash";
var urlobj = url.parse(urlstr);
console.log(urlobj);

/*
 Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'localhost:8888',
  port: '8888',
  hostname: 'localhost',
  hash: '#hash',
  search: '?n=big&m=he&m=cc',
  query: 'n=big&m=he&m=cc',
  pathname: '/a',
  path: '/a?n=big&m=he&m=cc',
  href: 'http://localhost:8888/a?n=big&m=he&m=cc#hash' }
*/

第二个参数为true时 :

Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'localhost:8888',
  port: '8888',
  hostname: 'localhost',
  hash: '#hash',
  search: '?n=big&m=he&m=cc',
  query: { n: 'big', m: [ 'he', 'cc' ] },
  pathname: '/a',
  path: '/a?n=big&m=he&m=cc',
  href: 'http://localhost:8888/a?n=big&m=he&m=cc#hash' }

url模块化:url.format()

将一个url对象转换成一个url字符串,url对象中的属性为url.parse()产生的对象的属性。
url.parse()和url.format()互为逆操作。
例如:

let url = require('url');
let Url={
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'localhost:8888',
  port: '8888',
  hostname: 'localhost',
  hash: '#hash',
  search: '?n=big&m=he&m=cc',
  query: { n: 'big', m: [ 'he', 'cc' ] },
  pathname: '/a',
  path: '/a?n=big&m=he&m=cc',
  href: 'http://localhost:8888/a?n=big&m=he&m=cc#hash' };
let str= url.format(Url);
console.log(str);
//http://localhost:8888/a?n=big&m=he&m=cc#hash

路径解析: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'

相关文章

  • nodejs之url模块使用

    官方手册上面的一张图是这样子的: 这张图解释了一个url结构化成哪些部分,哪些部分又包含哪些部分 protocol...

  • 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 是文件...

  • nodejs url模块详解

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

  • 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 中 http,url 模块使用

    1、http 模块 2、url 模块 简单例子

网友评论

    本文标题:nodejs之url模块使用

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