The
url
module and the querystring
module are two very important URL
processing modules. It is often used when doing node
server development.
Before introducing the url
module, let’s take a picture first. After understanding this picture, it is useful for url
You will basically have no problems with this module.
Let’s explain their respective meanings
:
, and is lowercase. [Related tutorial recommendations: nodejs video tutorial, Programming teaching]:
is followed by two //
, then it is true. usrname:passwd
, if not, it is usrname
. Note that this is case sensitive. ke.qq.com:8080
, and is in lowercase. ?
, in addition, the value is not decoded. search
with ?
removed, and the rest is the same; if it is an object, it is decoded.
# is included. protocol
and host
will be converted into lowercase letters. Let’s explain its three common methods
This method willurl
string is parsed into object
, which is convenient for developers to operate.
const url = require("url"); const str = "http://user:password@randy.com:8080/index.html?nick=%E4%B8%AD%E6%96%87#part=1"; const obj = url.parse(str); console.log(obj);
Output
##This method also supports passing two other parameters,parseQueryString and
slashesDenoteHos
parseQueryString: (default is false) If it is false, then
urlObject.query is an unparsed string, such as
nick= Chinese , and the corresponding value will not
decode; if
parseQueryString is true, then
urlObject.query is
object, for example
{ nick: 'Chinese' }, and the value will be `decoded;
const url = require("url"); const str = "http://user:password@randy.com:8080/index.html?nick=%E4%B8%AD%E6%96%87#part=1"; const obj2 = url.parse(str, true); console.log(obj2);
slashesDenoteHos: (default is false) If it is true, then
randy in
//randy/nick will be considered
hostname; if it is
false, then
randy is considered to be part of
pathname.
const str2 = "//randy/nick"; const obj3 = url.parse(str2, true, false); console.log(obj3); const obj4 = url.parse(str2, true, true); console.log(obj4);
parse. Convert the object into a
url string.
const pathObj = { protocol: "http:", slashes: true, auth: "user:password", host: "randy.com:8080", port: "8080", hostname: "randy.com", hash: "#part=1", search: "?nick=%E4%B8%AD%E6%96%87", query: "nick=%E4%B8%AD%E6%96%87", pathname: "/index.html", path: "/index.html?nick=%E4%B8%AD%E6%96%87", href: "http://user:password@randy.com:8080/index.html?nick=%E4%B8%AD%E6%96%87#part=1", }; console.log(url.format(pathObj)); // http://user:password@randy.com:8080/index.html?nick=%E4%B8%AD%E6%96%87#part=1
URL relative to the base
URL.
console.log(url.resolve("/one/two/three", "four")); // /one/two/four console.log(url.resolve("http://example.com/", "/one")); // http://example.com/one console.log(url.resolve("http://example.com/one", "/two")); // http://example.com/two console.log(url.resolve("http://example.com/one/ddd/ddd/ddd", "./two")); // http://example.com/one/ddd/ddd/two console.log(url.resolve("http://example.com/one/ddd/ddd/ddd", "../two")); // http://example.com/one/ddd/two console.log(url.resolve("http://example.com/one/ddd/ddd/ddd", ".../two")); // http://example.com/one/ddd/ddd/.../two
querystringThis module is also used to parse
url query parameters. Here we focus on analyzing its two methods
parse and
stringify.
parse is to convert the query string into an object type, and also
decode.
const querystring = require("querystring"); const str = "nick=randy&age=24&nick2=%E4%B8%AD%E6%96%87"; const obj = querystring.parse(str); console.log(obj); // { nick: 'randy', age: '24', nick2: '中文' }
& and = with custom characters. The author will give an example below and you will understand quickly.
const str1 = "name-randy|country-cn"; const obj1 = querystring.parse(str1); console.log(obj1); // { 'name-randy|country-cn': '' } const obj2 = querystring.parse(str1, "|", "-"); console.log(obj2); // { name: 'randy', country: 'cn' }
& with
|, and replacing
= with
-. The author feels that this situation should be rare.
parse. Let’s go directly to the example
const obj3 = { nick: "randy", age: "24", }; const str4 = querystring.stringify(obj3); console.log(str4); // nick=randy&age=24
const obj5 = { name: "randy", country: "cn", }; const str6 = querystring.stringify(obj5, "|", "-"); console.log(str6); // name-randy|country-c
更多node相关知识,请访问:nodejs 教程!
The above is the detailed content of Let's talk about the url module and querystring module in Node. For more information, please follow other related articles on the PHP Chinese website!