NodeJS---URL-related module usage (URL and querystring)
One: URL module:
The URL module is used to parse and process a URL string, providing the following three methods:
1. Parse2. Format3. Resolve
1.1 Url.parse (urlstring); Convert the URL string address to an object.
The following code:
Const URL = require (' URL '); Const URLString= Url.parse (' http://www.nodejs.org/some/url/?with=query¶m=that#about '); Console.log (urlstring); output is as follows://Output:URL {protocol:' http: ', slashes:true, Auth:NULL, Host:' Www.nodejs.org ', Port:NULL, hostname:' Www.nodejs.org ', Hash:' #about ', search:'? With=query¶m=that ', query:' With=query¶m=that ', Pathname:'/some/url/', Path:'/some/url/?with=query¶m=that ', href:' Http://www.nodejs.org/some/url/?with=query¶m=that#about '}
Protocal:url protocol
Auth: User authentication
Host: Hosts
Port: Ports
Hostname: Host Name
Hash: Fragment part, that is, the part after url#
Information about HTTP get in Search:url, including?
Query: Same as search, but not included?
Pathname: The entire file path following the host. But not included? And then the string.
Path: Same as pathname, but included? and subsequent strings, but no hash
HREF: the original URL
1.2 The Format method.
The format method, in contrast to the parse method, is used to generate a string of URLs based on an object.
For example, the following code:
Const URL = require (' URL ');varUrlobj ={protocol:' http: ', slashes:true, Auth:NULL, Host:' Www.nodejs.org ', Port:NULL, hostname:' Www.nodejs.org ', Hash:' #about ', search:'? With=query¶m=that ', query:' With=query¶m=that ', Pathname:'/some/url/', Path:'/some/url/?with=query¶m=that ', href:' Http://www.nodejs.org/some/url/?with=query¶m=that#about '};console.log (Url.format (urlobj));
Last output:http://www.nodejs.org/some/url/?with=query¶m=that#about
1.3 Resolve Method
The resolve (from, to) method is used for stitching URLs, which are spliced into new URLs based on relative URLs;
The following code:
Const URL = require (' URL ');varstr1 = Url.resolve ('/one/two/three ', ' four '); Console.log (STR1); //Output/one/two/fourConst STR2= Url.resolve (' http://www.baidu.com ', ' four '); Console.log (STR2); //Output Http://www.baidu.com/fourConst STR3= Url.resolve (' http://www.baidu.com/', '/four '); Console.log (STR3); //Output Http://www.baidu.com/fourConst STR4= Url.resolve (' http://www.baidu.com/one/', '/four '); Console.log (STR4); //Output Http://www.baidu.com/four
II: QueryString Module
It is used to parse and format URL query strings.
It provides four methods, namely:Querystring.parse, Querystring.stringify, Querystring.escape and Querystring.unescape;
2.1 Querystring.parse (string, separator, EQ, options);
The method is to deserialize a string into an object.
String: Refers to strings that need to be deserialized.
Separator (optional): Refers to the character used to split string strings, &; by default
EQ (optional): refers to the character and string used to divide the key and value, and the default value is "=";
Options (optional): This parameter is an object in which you can set both Maxkeys and decodeURIComponent properties.
Maxkeys: Passes in a nmuber type, specifying the maximum value for resolving key-value pairs, the default value is 1000, and if set to 0, the number limit for resolution is canceled.
decodeURIComponent: A function is passed in to encode a special character.
The following code:
Const URL = require (' URL '); Const QueryString= Require (' querystring '); Const URLString= Url.parse (' http://www.nodejs.org/some/url/?with=query¶m=that#about '); Console.log (urlstring);/*{protocol: ' http: ', Slashes:true, Auth:null, Host: ' Www.nodejs.org ', Port:null, hostname: ' www.nodejs.org ', Hash: ' #about ', search: '? with=query¶m=that ', query: ' With=query¶m=that ', pathname: '/some/url/', PA Th: '/some/url/?with=query¶m=that ', href: ' http://www.nodejs.org/some/url/?with=query¶m=that# About '}}*/Const STR=Querystring.parse (urlstring.query); Console.log (str);/*return {with: ' Query ', param: ' That '}*/
If it is separated by a pound sign, then use the following parameters
const CHAR = "With=query#param=that";
Output {with: ' Query ', param: ' That '}
Const STR2 = querystring.parse (char, ' # ', null, {maxkeys:2});
2.2 Querystring.stringify (obj, separator, EQ, options);
The method is to serialize an object into a string.
Parameter: obj refers to the object that needs to be serialized;
Separator (optional), character or string used to connect key-value pairs, &; by default
EQ (optional), the character or string that is used to connect the key and value, and the default value is "=";
Options (optional), passing in an object that sets the encodeURIComponent property;
encodeURIComponent: The type of the value is function, you can convert an unsafe URL string to a percentage, the default value is Querystring.escape ().
The following code:
Const URL = require (' URL '); Const QueryString= Require (' querystring ');//if it is separated by a pound sign, then use the following parametersConstChar= "With=query#param=that"; Const STR2= Querystring.parse (Char, ‘#‘,NULL, {Maxkeys:2});//output {with: ' Query ', param: ' That '}Const STR3=querystring.stringify (STR2); Console.log (STR3);//Output With=query¶m=that//Use the * number to split, use the $ symbolic linkConst STR4 = querystring.stringify ({name: ' Kongzhi ', sex: [' man ', ' Women ']}, "*", "$"); Console.log (STR4); //Name$kongzhi*sex$man*sex$women
2.3 Querystring.escape (str);
Escape This method encodes the passed-in string, as in the following code:
Const URL = require (' url '= require (' querystring '); // if it is separated by a pound sign, then use the following parameters char = "Name= &sex= man"= Querystring.escape (char); Console.log (res); // Output Name%3d%e7%a9%ba%e6%99%ba%26sex%3d%e7%94%b7
2.4 Querystring.unescape (str);
This method decodes a character that uses escape encoding, as follows:
Const URL = require (' url '= require (' querystring '); // if it is separated by a pound sign, then use the following parameters char = "Name= &sex= man"= Querystring.escape (char); Console.log (res); // Output Name%3d%e7%a9%ba%e6%99%ba%26sex%3d%e7%94%b7 =// name= &sex= male
NodeJS---URL-related module usage (URL and querystring)