3.nodejs的URL模块

1.引入

var url = rquire("url")

2.相关方法使用

url.parse("地址路径" , true , true)将域名字符串进行解析,返回一个对象,后面两个参数为可选参数

var obj = url.parse("https://www.baidu.com:3000/html/index.html?username=sune#cdf" , true , true)

console.log(obj);

obj{

    protocal:"https", //协议名

    slashes:"true",  //是否有双斜线

    auth:null,

    host:"www.baidu.com:3000",  //主机域名,带端口号,前提是有端口号

    port:"300",  //端口号

    hostname:"www.baidu.com",  //主机域名不带端口号

    hash:"#cdf",  //hash值#后面的

    search:"?username=sune",  //获取地址中的参数

    query:{username:sune},  //获取地址中的参数不带?

    pathname:"/html/index.html",  //页面路径不带参数

    path:"/html/index.html?username=sune",  //带参数的页面路径

    href:"https://www.baidu.com:3000/html/index.html?username=sune#cdf" //完整的路径

}

url.parse("路径",true , true)

当第二个参数为true时query的属性值为一个对象,方便取用

当第三个参数为true时,可解析没有协议名的地址路径

即当protocal:null时hostname和host仍然正常解析

你可能感兴趣的:(3.nodejs的URL模块)