node. url Module for js Getting Started
- 2021-07-24 09:41:25
- OfStack
Preface
Today's main record is about node. js inside a simple module, url module. This url module needs to be introduced first if it is to be used. If you only use the module url on the command line, such as cmd, git, bash, etc., you don't need require to come in. Just use it directly.
Introducing module
var url = require('url');
1. url.parse()
/* url.parse(urlString[,parseQueryString[,slashesDenoteHost]])
* urlString <string> The URL to be resolved
* parseQueryString <boolean> Default to false , will query Analyze into 1 Strings; How to set to true Will the query Analyze into 1 Objects
* @return <object>
*/
var result = url.parse('https://www.baidu.com/s?ie=UTF-8&wd=node.js')
console.log(result);
// Print results
{
protocol: 'https:', // url Agreement
slashes: true, // Slant line '/'
auth: null, // User authentication
host: 'www.baidu.com', // Host
port: null, // Port
hostname: 'www.baidu.com', // Hostname
hash: null, // hash Value
search: '?ie=UTF-8&wd=node.js', // url Query information in, including '?'
query: 'ie=UTF-8&wd=node.js', // url Query information in, excluding '?'
pathname: '/s', // Follow host The entire file path after
path: '/s?ie=UTF-8&wd=node.js', // pathname And search
href: 'https://www.baidu.com/s?ie=UTF-8&wd=node.js' // Element url
}
2. url.format(urlObject)
The format method, as opposed to the parse method, is used to generate an url from an object
var urlObj = {
protocol:'http',
host:'www.baidu.com',
pathname:'/page',
search:'?index=1&sign=true'
}
var urlStr = url.format(urlObj);
console.log(urlStr);
// Print results
// http://www.baidu.com/page?index=1&sign=true
3. url.resolve(from,to)
Used to splice url
var urlStr = url.resolve('http://www.baidu.com/','page');
console.log(urlStr);
// Print results
// http://www.baidu.com/page
var urlStr1 = url.resolve('/page/person/','pic');
console.log(urlStr1);
// Print results
// /page/person/pic
Summarize