node built-in module

node built-in module

url

url module commonly used in the address resolution request
usr.parse (url, bool)
only when the two reference resolves to true, the object in the url parameter query properties

const url = require('url');
const strUrl1 = 'http://www.lagou.com/a/b?name=zhangsan&pswd=123456';
var rs1 = url.parse(strUrl1);
var rs2 = url.parse(strUrl1,true);//true把get请求参数解析成对象
console.log(rs1);
console.log(rs2);

Print results

url.format (paramobj)
to generate the object based on the parameters url

const url = require('url');
var params = {
    protocol:'https:',
    host:'www.lagou.com',
    hash:'#position',
    search:'?name=zhangsan%age=27',
    pathname:'/a/b'
}
//根据参数对象生成路径
var rs = url.format(params);
console.log(rs);//https://www.lagou.com/a/b?name=zhangsan%age=27#position

url.resolve (url, replaceStr)
to replace the contents of a url / after

const url = require('url');
const strUrl = 'http://www.lagou.com/a/b?name=zhangsan&pswd=123456';
console.log(url.resolve(strUrl,'!!!replace!!!'));//http://www.lagou.com/a/!!!replace!!!

path

  • path.resolve (__ dirname, replaceStr) __ dirname built-in objects is a node representing the current path environment. The method is used to add back to the specified path replacestr
const path = require('path');
var rs = path.resolve(__dirname,"!!!replace!!!")
console.log(rs);//D:\part3\day0316\server\!!!replace!!!

querystring

querystring.parse (url)
for converting an object parameter request url

const qs = require('querystring');
const strUrl = 'http://www.lagou.com/a/b?name=zhangsan&pswd=123456';
var rs = qs.parse(strUrl);
console.log(rs); //{ 'http://www.lagou.com/a/b?name': 'zhangsan', pswd: '123456' }

Guess you like

Origin www.cnblogs.com/ltfxy/p/12508091.html