Home > Web Front-end > JS Tutorial > body text

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

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

The

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

url module and the querystring module are two very important URL processing modules. It is often used when doing node server development.

url

Before introducing the url module, let’s take a picture first. After understanding this picture, it is useful for urlYou 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 is usrname:passwd, if not, it is usrname. Note that this is case sensitive.
  • host: host name. Note that the port is included, such as ke.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 is search with ? 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 that protocol and host will be converted into lowercase letters.

Let’s explain its three common methods

parse(urlString, parseQueryString, slashesDenoteHost)

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

Output

Lets talk about the url module and querystring module in Node

##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);
Copy after login

Lets talk about the url module and querystring module in Node

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.

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

resolve(from, to)

This method is used to resolve the target

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

querystring

querystringThis module is also used to parse url query parameters. Here we focus on analyzing its two methods parse and stringify.

parse(str, sep, eq, options)

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: '中文' }
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template