Home >Web Front-end >JS Tutorial >Detailed explanation of converting URL strings and query strings in nodejs_node.js
In a complete URL string, the part from "?" (excluding ?) to "#" (if # exists) or to the end of the URL string (if # does not exist) is called the query string .
You can use the parse method in the Query String module to convert the string into an object. The parse method is used as follows:
querystring.parse(str,[sep],[eq],[options]);
str represents the converted query string,
sep. The delimiter in the string, the default is &
eq. The allocation character in this string, the default is =."="The left side is key and the right side is value
options: is an object. You can use the maxKeys attribute of an integer value type in the object to specify the number of attributes in the converted object. If the maxKeys attribute value is set to 0, the effect is equivalent to not using maxKeys. Attribute value
Stringify is a format that converts strings into query strings.
querystring.stringify(obj,[sep],[eq])
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 attributes that the object may have and their meanings are as follows.
href: The converted original URL string.
protocol: The protocol used by the client when making requests.
slashes: Use the "//" separator between the protocol and the path.
host: The complete address and port number in the URL string. The address may be an IP address or a host name.
auth: The authentication information part in the URL string.
hostname: The complete address in the URL string, which may be an IP address or a host name.
search: The query string in the Url string, including the starting character "?"
path: The path in the url string, including the query string.
query: The query string in the url string, does not contain the starting character "?", or the object converted based on the query string (the query attribute value is determined based on the parameters used in the parse() method);
hash: the hash string in the url string, including the starting character "#".
url.parse(urlstr,[parseQueryString]);
urlStr: is the 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 difference between the first example and the second example is the second parameter of parse, which results in a different query in the result
You can convert a url converted object into a url string.
The result is:
http://user:pass@host:8080/,com/users/user.php?username=sisi&age=24&sex=male#name1
The above is all about converting URL strings and query strings in node. If you study it carefully, it is actually quite simple.