Let's talk about the url module and querystring module in Node

青灯夜游
Release: 2023-02-23 19:39:25
forward
1884 people have browsed it

The

Let's talk about the url module and querystring module in Node

urlmodule and thequerystringmodule are two very importantURLprocessing modules. It is often used when doingnodeserver development.

url

Before introducing theurlmodule, let’s take a picture first. After understanding this picture, it is useful forurlYou will basically have no problems with this module.

Lets talk about the url module and querystring module in Node

Let’s explain their respective meanings

  • protocol: protocol. It should be noted that it contains:, and is lowercase. [Related tutorial recommendations:nodejs video tutorial,Programming teaching]
  • slashes: If:is followed by two//, then it is true.
  • auth: Authentication information, if there is a password, it isusrname:passwd, if not, it isusrname. Note that this is case sensitive.
  • host: host name. Note that the port is included, such aske.qq.com:8080, and is in lowercase.
  • hostname: hostname, does not include the port, and is lowercase.
  • port: Port number.
  • path: The path part, including the search part.
  • pathname: path part, does not include the search part.
  • search: query string, note that it contains?, in addition, the value is not decoded.
  • query: string or object. If it is a string, it issearchwith?removed, and the rest is the same; if it is an object, it is decoded.
  • hash: The hash part, please note that# is included.
  • href: Original address. However, it should be noted thatprotocolandhostwill be converted into lowercase letters.

Let’s explain its three common methods

parse(urlString, parseQueryString, slashesDenoteHost)

This method willurlstring is parsed intoobject, 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);
Copy after login

Output

Lets talk about the url module and querystring module in Node

##This method also supports passing two other parameters,

parseQueryStringandslashesDenoteHos

parseQueryString: (default is false) If it isfalse, thenurlObject.queryis an unparsed string, such asnick= Chinese, and the corresponding value will notdecode; ifparseQueryStringis true, thenurlObject.queryisobject, 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);
Copy after login

Lets talk about the url module and querystring module in Node

slashesDenoteHos: (default is false) If it istrue, thenrandyin//randy/nickwill be consideredhostname; if it isfalse, thenrandyis considered to be part ofpathname.

It may seem that you don’t understand the meaning of this sentence. I will give you an example below and I believe you will understand.

const str2 = "//randy/nick"; const obj3 = url.parse(str2, true, false); console.log(obj3); const obj4 = url.parse(str2, true, true); console.log(obj4);
Copy after login

Lets talk about the url module and querystring module in Node

format(urlObject)

This method is the reverse operation of

parse. Convert the object into aurlstring.

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
Copy after login

resolve(from, to)

This method is used to resolve the target

URLrelative to the baseURL.

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
Copy after login

querystring

querystringThis module is also used to parseurlquery parameters. Here we focus on analyzing its two methodsparseandstringify.

parse(str, sep, eq, options)

parseis to convert the query string into an object type, and alsodecode.

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: '中文' }
Copy after login

Let’s take a look at its second and third parameters. In fact, it is equivalent to replacing

& 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' }
Copy after login

is equivalent to replacing

&with|, and replacing=with-. The author feels that this situation should be rare.

stringify(obj, sep, eq, options)

This method is the reverse operation of the above

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
Copy after login

This method also supports custom separators.

const obj5 = { name: "randy", country: "cn", }; const str6 = querystring.stringify(obj5, "|", "-"); console.log(str6); // name-randy|country-c
Copy after login

更多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!

Related labels:
source:juejin.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!