Home  >  Article  >  Web Front-end  >  Detailed explanation of converting URL strings and query strings in nodejs_node.js

Detailed explanation of converting URL strings and query strings in nodejs_node.js

WBOY
WBOYOriginal
2016-05-16 16:30:141587browse

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

Copy code The code 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 a format that converts strings into query strings.

querystring.stringify(obj,[sep],[eq])

Copy code The code 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 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

Copy code The code 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);

Copy code The code 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' }

Copy code The code 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);

Copy code The code 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 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.

Copy code The code 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));

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn