This part of a complete URL string from "?" (excluding?) to "#" (if # exists) or to the end of the URL string (if # does not exist) is called a query string.
You can use the parse method in the Query String module to convert the string into an object. The use of the parse method is as follows:
querystring.parse(str,[sep],[eq],[options]);
str represents the converted query string,
The separator in the sep. string, the default is &
eq. The allocation character in this string defaults to =."="The left is key, and the right is value.
options: is an object, in which a maxKeys attribute of an integer value type can be used to specify the number of attributes in the converted object. If the maxKeys attribute value is set to 0, its effect is equal to not using the maxKeys attribute value.
The code copy is as follows:
var querystring=require("querystring");
var str="username=guoyansi&age=40&sex=male";
var res=querystring.parse(str);
console.log("1:%j",res);//1:{"username":"guoyansi","age":"40","sex":"male"}
res=querystring.parse(str,"!");
console.log("2:%j",res);//2:{"username":"guoyansi&age=40&sex=male"}
res=querystring.parse(str,"&");
console.log("3:%j",res);//3:{"username":"guoyansi","age":"40","sex":"male"}
str="username=guoyansi!age=40!sex=male";
res=querystring.parse(str,"!");
console.log("4:%j",res);//4:{"username":"guoyansi","age":"40","sex":"male"}
res=querystring.parse(str,"!","=");
console.log("5:%j",res);//5:{"username":"guoyansi","age":"40","sex":"male"}
res=querystring.parse(str,"!",":");
console.log("6:%j",res);//6:{"username=guoyansi":"","age=40":"","sex=male":""}
res=querystring.parse(str,"!","=",{maxKeys:2});
console.log("7:%j",res);//7:{"username":"guoyansi","age":"40"}
stringify is the format for converting strings into query strings.
querystring.stringify(obj,[sep],[eq])
The code copy is as follows:
var querystring=require("querystring");
var res= querystring.stringify({"username":"guoyansi","age":"40","sex":"male"});
console.log(res);//username=guoyansi&age=40&sex=male
res=querystring.stringify({"username":"guoyansi","age":"40","sex":"male"},"!");
console.log(res);//username=guoyansi!age=40!sex=male
res=querystring.stringify({"username":"guoyansi","age":"40","sex":"male"},"&",":");
console.log(res);//username:guoyansi&age:40&sex:male
res=querystring.stringify({"username":"guoyansi","age":["40","24"]},"&","=");
console.log(res);//username=guoyansi&age=40&age=24
In the url module, you can use the parse() method to convert the URL string into an object. Depending on the different contents in the URL string, the possible properties and their meanings of the object are as follows.
href: The converted original URL string.
protocol: The protocol used by the client when making a request.
slashes: Use the "//" separator between the protocol and the path.
host: The complete address and port number in the URL string. This address may be an IP address or a host name.
auth: Authentication information part in the URL string.
hostname: The complete address in the URL string, which may be an IP address or a hostname.
search: The query string in the Url string, containing the starting character "?"
path: path in url string, containing query string.
query: The query string in the url string does not contain the starting character "?", or an object converted based on the query string (the query attribute value is determined based on the parameters used by the parse() method);
hash: hash string in the url string, containing the starting character "#".
url.parse(urlstr,[parseQueryString]);
urlStr: is a URL string that needs to be converted.
parseQueryString: is a boolean value. When the parameter is true, the querystring module is used internally to convert the query string into an object. When the parameter value is false, the conversion operation is not performed. The default is false.
The code copy is as follows:
var url=require("url");
var str="http://user:pass@host,com:8080/users/user.php?username=sisi&age=24&sex=male#name1";
var res=url.parse(str);
console.log(res);
The code copy is as follows:
{ protocol: 'http:',
slashes: true,
auth: 'user:pass',
host: 'host:8080',
port: '8080',
hostname: 'host',
hash: '#name1',
search: '?username=sisi&age=24&sex=male',
query: 'username=sisi&age=24&sex=male',
pathname: '/,com/users/user.php',
path: '/,com/users/user.php?username=sisi&age=24&sex=male',
href: 'http://user:pass@host:8080/,com/users/user.php?username=sisi&age=24&sex=male#name1' }
The code copy is as follows:
var url=require("url");
var str="http://user:pass@host,com:8080/users/user.php?username=sisi&age=24&sex=male#name1";
var res=url.parse(str,true);
console.log(res);
The code copy is as follows:
{ protocol: 'http:',
slashes: true,
auth: 'user:pass',
host: 'host:8080',
port: '8080',
hostname: 'host',
hash: '#name1',
search: '?username=sisi&age=24&sex=male',
query: { username: 'sisi', age: '24', sex: 'male' },
pathname: '/,com/users/user.php',
path: '/,com/users/user.php?username=sisi&age=24&sex=male',
href: 'http://user:pass@host:8080/,com/users/user.php?username=sisi&age=24&sex=male#name1' }
The difference between the first example and the second example is that the second parameter of parse leads to the difference in query in the result
You can convert an url converted object into a url string.
The code copy is as follows:
var url=require("url");
var str="http://user:pass@host,com:8080/users/user.php?username=sisi&age=24&sex=male#name1";
var res=url.parse(str,true);
console.log(url.format(res));
turn out:
http://user:pass@host:8080/,com/users/user.php?username=sisi&age=24&sex=male#name1
The above is all the contents of converting URL strings and query strings in node. After careful study, it is actually quite simple.