1 我们可以使用.parse方法来将一个URL字符串转换为URL对象

例如:

url.parse('http://user:pass@host.com:8080/p/a/t/h?query=string#hash');
/* =>
{ protocol: 'http:',
  auth: 'user:pass',
  host: 'host.com:8080',
  port: '8080',
  hostname: 'host.com',
  hash: '#hash',
  search: '?query=string',
  query: 'query=string',
  pathname: '/p/a/t/h',
  path: '/p/a/t/h?query=string',
  href: 'http://user:pass@host.com:8080/p/a/t/h?query=string#hash' }
*/

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

接收参数:

urlStr                                       url字符串

parseQueryString                   为true时将使用查询模块分析查询字符串,默认为false

slashesDenoteHost

默认为false,//foo/bar 形式的字符串将被解释成 { pathname: ‘//foo/bar' }

如果设置成true,//foo/bar 形式的字符串将被解释成  { host: ‘foo', pathname: ‘/bar' }

例子:

var url = require('url');
var a = url.parse('http://example.com:8080/one?a=index&t=article&m=default');
console.log(a);
 
//输出结果:
{
    protocol : 'http' ,
    auth : null ,
    host : 'example.com:8080' ,
    port : '8080' ,
    hostname : 'example.com' ,
    hash : null ,
    search : '?a=index&t=article&m=default',
    query : 'a=index&t=article&m=default',
    pathname : '/one',
    path : '/one?a=index&t=article&m=default',
    href : 'http://example.com:8080/one?a=index&t=article&m=default'
}

如果parseQueryString 设置为true  url对象中的query会变成一个对象,如:  query:{a:"index",t::"article",m:"default"}

2 .resolve方法可以用于拼接URL

url.resolve('http://www.example.com/foo/bar', '../baz');
/* =>
 
http://www.example.com/baz
 
*/

3 反过来,.format方法允许将一个URL对象转换为URL字符串

url.format({
    protocol: 'http:',
    host: 'www.example.com',
    pathname: '/p/a/t/h',
    search: 'query=string'
});
/* =>
'http://www.example.com/p/a/t/h?query=string'
*/

URL 参数说明:

{

     protocol: 'http:',

     slashes: true,

     auth: null,

     host: 'localhost:8888',

     port: '8888',

     hostname: 'localhost',

     hash: null,

     search: '?name=bigbear&memo=helloworld',

     query: 'name=bigbear&memo=helloworld',

     pathname: '/bb',

     path: '/bb?name=bigbear&memo=helloworld',

     href: 'http://localhost:8888/bb?name=bigbear&memo=helloworld'

 } 

  protocol: 请求协议

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

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

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

  port: 主机的端口号部分

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

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

  path: pathname 和 search 连在一起。

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

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

nodejs URL 详解的更多相关文章

  1. Fiddler抓包6-get请求(url详解)

    前言 上一篇介绍了Composer的功能,可以模拟get和post请求,get请求有些是不带参数的,这种比较容易,直接放到url地址栏就行.有些get请求会带有参数,本篇详细介绍url地址格式. 一. ...

  2. Nodejs this详解

    [Nodejs this详解] Nodejs中, 文件层this,指向的是module.export. 函数层this,指向的是global对象. 参考:http://www.jb51.net/art ...

  3. Fiddler抓包6-get请求(url详解)【转载】

    本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/fiddler/ 前言 上一篇介绍了Composer的功能,可以模拟get和post请求 ...

  4. 3、get请求(url详解)

    前言 上一篇介绍了Composer的功能,可以模拟get和post请求,get请求有些是不带参数的,这种比较容易,直接放到url地址栏就行.有些get请求会带有参数,本篇详细介绍url地址格式. 一. ...

  5. 基础篇-http协议《http 简介、url详解、request》

    目录 一.http 简介 二.url 详解 三.request 1.get 和 post 2.请求方法 3.request 组成 4.请求头 5.get 请求参数 6.post 请求参数 7.post ...

  6. URI与URN与URL详解

    当没有URI时 什么是URI和URN和URL URI详解 Uniform Resource Identifier 统一资源标识符 URI的组成 案例: https://tools.ietf.org/h ...

  7. 【基础进阶】URL详解与URL编码

    作为前端,每日与 URL 打交道是必不可少的.但是也许每天只是单纯的用,对其只是一知半解,随着工作的展开,我发现在日常抓包调试,接口调用,浏览器兼容等许多方面,不深入去理解URL与URL编码则会踩到很 ...

  8. URL详解与URL编码

    作为前端,每日与 URL 打交道是必不可少的.但是也许每天只是单纯的用,对其只是一知半解,随着工作的展开,我发现在日常抓包调试,接口调用,浏览器兼容等许多方面,不深入去理解URL与URL编码则会踩到很 ...

  9. Nodejs mongoose 详解

    前言 Mongoose 是在nodejs环境下,对mongodb进行便捷操作的对象模型工具.本文介绍解(翻)密(译)Mongoose插件. Schema 开始我们就要讲到Schema,一个Schema ...

随机推荐

  1. [转] Python 常用第三方模块 及PIL介绍

    原文地址 除了内建的模块外,Python还有大量的第三方模块. 基本上,所有的第三方模块都会在PyPI - the Python Package Index上注册,只要找到对应的模块名字,即可用pip ...

  2. uibutton去掉点击后背景有阴影的方法

    1,将normal和highlight两种方式都设置上图片即可 UIButton *goback = [[UIButton alloc]initWithFrame:CGRectMake(5.0f, 5 ...

  3. 微软CIO如何与业务部门打交道?

    微软公司副总裁兼CIO Tony Scott是一个非常智慧的人,他拒绝和CEO讨论IT成本的问题,认为IT不应该谈论成本,而是应该谈论IT提供服务的价值.在满足业务部门需求.为业务部门提供适当的IT支 ...

  4. Thrift --- 支持双向通信

    [问题] Thrift采用了C/S模型,不支持双向通信:client只能远程调用server端的RPC接口,但client端则没有RPC供server端调用,这意味着,client端能够主动与serv ...

  5. 第24章、OnLongClickListener长按事件(从零开始学Android)

    在Android App应用中,OnLongClick事件表示长按2秒以上触发的事件,本章我们通过长按图像设置为墙纸来理解其具体用法. 知识点:OnLongClickListener OnLongCl ...

  6. c++单元测试指南:使用google test

    Reference:http://www.codeproject.com/Articles/811934/Cplusplus-unit-test-start-guide-how-to-set-up-G ...

  7. 关于 thinkPHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback

    Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback 关于thinkPHP rpc调 ...

  8. reorder-list——链表、快慢指针、逆转链表、链表合并

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  9. c程序设计语言第一章5

    练习1.20请编写程序d e t a b

  10. Kubernetes实战阅读笔记--1、介绍

    1.业界根据云计算提供服务资源的类型将其划分为三大类: 基础设施即服务(Infrastructure-as-a-Service,IaaS).平台即服务(Platform-as-a-Service,Pa ...