Node Series URL Module

Source: Internet
Author: User

Introduction URL:
   Const URL = require ("url");

Solutions for URL parsing, processing, and more

1.url.parse (urlstr[, parsequerystring][, Slashesdenotehost])

Converting URL strings to object objects

/**
* Urlstr: URL string to process
* Parsequerystring: Whether to parse query parameters into objects
* True when query string is parsed using query module, default is False
* Slashesdenotehost: Parsing host processing, double slash indicates host
* The default is False,//foo/bar form string will be interpreted as {pathname: '//foo/bar '}
* If the string set to True,//foo/bar form will be interpreted as {host: ' foo ', pathname: '/bar '}
*/

Instance 1, specifying only URL characters to parse into objects

var url=require(‘url‘);  var url1=‘http://calc.gongjuji.net/byte/?name=zhangsan&age=18#one#two‘;  var result=url.parse(url1);   { protocol: ‘http:‘,    //使用协议      slashes: true,        //  auth: null,           // 验证信息  host: ‘calc.gongjuji.net‘, //全小写的主机部分的URL,包括端口信息。  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‘  //最初解析的完整的网址。双方的协议和主机是小写。   }   

Instance 2, specifying whether to parse the parameter part into an object

//参数解析  var result2=url.parse(url1,true);   { 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‘ }  

Example 3, host special parsing

//双斜线表示主机  var url2=‘//www.gongjuji.net/byte/?name=zhangsan#one‘;   {   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 result3=url.parse(url2,true,true);        console.info(result3);  

2.url.format (urlobj) formats a JSON object as a string

var url=require (' url '); var obj1={protocol: ' http: ', Slashes:true, Auth:null, Host: ' Calc.gongjuji.net ', por T: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//Request parameter is a JSON object 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 '},//page parameter part, has been parsed into an object Pathna Me: '/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//Missing parameters in case var obj3={protocol:null, Slashes:true, Auth:null, Host: ' Www.gongjuji.net ', Port:null, hostname: ' www.gongjuji.net ', hash: ' #one ', search: '? Name=zhangsa n ', query: {name: ' Zhangsan '}, Pathname: '/byte/', Path: '/byte/?name=zhangsan ', href: '//www.gongjuji.net/byte/?nam  E=zhangsan#one '};  var url3=url.format (OBJ3);   Console.log (URL3);//www.gongjuji.net/byte/?name=zhangsan#one

3.url.resolve (from, to)

Returns the absolute path URL specified from the root directory to the current directory
1. Return results removed parameters and anchor points
2. Return the result standard URL path format

var url=require(‘url‘);  //指定相对路径  var url1=url.resolve(‘http://www.gongjuji.net/one/two/three‘,‘four‘);  console.log(url1); //http://www.gongjuji.net/one/two/four  //指定根目录的相对路径  var url3=url.resolve(‘http://www.gongjuji.net/one/two/three‘,‘/four‘);  console.log(url3); //http://www.gongjuji.net/four  //带参数的相对路径  var url2=url.resolve(‘http://www.gongjuji.net/one/two/three?name=zhangsan‘,‘four‘);  console.log(url2); //http://www.gongjuji.net/one/two/four  //非标准分隔符的原路径  var url4=url.resolve(‘http://www.gongjuji.net\\one#name1‘,‘/four‘);  console.log(url4);//http://www.gongjuji.net/four  //非标准分隔符的相对路径  var url5=url.resolve(‘http://www.gongjuji.net/one‘,‘\\two\\three‘);  console.log(url5);//http://www.gongjuji.net/two/three  

 

Node Series URL Module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.