Let's talk about the url module and querystring module in Node
The
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 url
You will basically have no problems with this module.
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 isusrname
. 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
andhost
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);
Output
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);
format(urlObject)
This method is the reverse operation ofparse. 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
resolve(from, to)
This method is used to resolve the targetURL 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
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: '中文' }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' }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 aboveparse. Let’s go directly to the example
const obj3 = { nick: "randy", age: "24", }; const str4 = querystring.stringify(obj3); console.log(str4); // nick=randy&age=24This method also supports custom separators.
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!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
