在撰寫url模塊代碼之前先安裝一個查看日志的依賴
在當前檔案目錄下先初始化一個package.json
npm init -y
然后在生產環境安裝日志依賴包安裝命令
npm install log4js -D
url一共提供了三個方法
url.parse();方法可以將一個url的字串決議并回傳一個url的物件
引數:urlString指傳入一個url地址的字串
var log4js = require("log4js");
log4js.configure({
appenders: { cheese: { type: "file", filename: "cheese.log" } },
categories: { default: { appenders: ["cheese"], level: "error" } }
});
var logger = log4js.getLogger('cheese');
logger.level = "debug";
const url= require("url")
const urlString = "https://www.baidu.com:443/path/index.html?id=2#tag=3"
logger.debug(url.parse(urlString))
// parse決議地址 new URL
// parse這個方法可以將一個url的字串決議并回傳一個url的物件
寫入以上代碼之后運行發現生成了一個cheese.log 的日志檔案

點進入之后可以發現在里面可以很清楚的看到你在哪個時間段做了哪些修改

轉換結果發現在cheese.log中發現parse方法將字串地址轉換成了物件
url.format();這個方法是將傳入的url物件編程一個url字串并回傳
與parse正好相反
var log4js = require("log4js");
log4js.configure({
appenders: { cheese: { type: "file", filename: "cheese.log" } },
categories: { default: { appenders: ["cheese"], level: "error" } }
});
var logger = log4js.getLogger('cheese');
logger.level = "debug";
const url= require("url")
const urlObj={
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.baidu.com:443',
port: '443',
hostname: 'www.baidu.com',
hash: '#tag=3',
search: null,
query: null,
pathname: '/path/index.html:id=2',
path: '/path/index.html:id=2',
href: 'https://www.baidu.com:443/path/index.html:id=2#tag=3'
}
logger.debug(url.format(urlObj))
轉換結果

url.resolve()方法用于拼接URL,它根據相對URL拼接成新的URL
var log4js = require("log4js");
log4js.configure({
appenders: { cheese: { type: "file", filename: "cheese.log" } },
categories: { default: { appenders: ["cheese"], level: "error" } }
});
var logger = log4js.getLogger('cheese');
logger.level = "debug";
const url= require("url")
const urlString = "https://www.baidu.com:443/path/index.html?id=2#tag=3"
logger.debug(url.resolve('http://www.abc.com', '/b'))
轉換結果

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/299692.html
標籤:其他
上一篇:HTML+CSS+JS實作 ??年年有魚祝福背景特效??
下一篇:??最新??2021國賽??B乙醇偶合制備 C4 烯烴??C生產企業原材料的訂購與運輸??E中藥材的鑒別??全套(LW,DM)
