從零開始學(xué)習(xí)Node.js
url模塊
1.parse 方法
// test02.js
import http from 'http'
import url from 'url'
const parseUrl = url.parse('https://www.baidu.com/news?name=諸葛亮&age=18#helloworld')
console.log(parseUrl)
http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'})
res.write('你好, hello world!')
res.end()
}).listen(3000)
console.log('My server is running at http://localhost:3000')
解析url地址,獲得一個(gè)被解析的url詳情對(duì)象,包含協(xié)議、域名、路徑、端口、查詢參數(shù)、哈希等信息。

第二個(gè)參數(shù)為boolean值,默認(rèn)為false,傳true會(huì)將query轉(zhuǎn)為對(duì)象
const parseUrl = url.parse('https://www.baidu.com/news?name=諸葛亮&age=18#helloworld', true)
console.log(parseUrl)

2.format 方法
傳入一個(gè)url信息對(duì)象(即parse方法返回的對(duì)象),返回一個(gè)具體的路徑,該方法是parse方法的逆運(yùn)用。
const formatUrl = url.format({
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.baidu.com',
port: null,
hostname: 'www.baidu.com',
hash: '#helloworld',
search: '?name=諸葛亮&age=18',
query: 'name=諸葛亮&age=18',
pathname: '/news',
path: '/news?name=諸葛亮&age=18',
href: 'https://www.baidu.com/news?name=諸葛亮&age=18#helloworld'
})
console.log(formatUrl) // 輸出 https://www.baidu.com/news?name=諸葛亮&age=18#helloworld
3.resolve 方法
拼接或替換次級(jí)路徑
const result1 = url.resolve('https://www.baidu.com', 'news')
const result2 = url.resolve('https://www.baidu.com/home', '')
const result3 = url.resolve('https://www.baidu.com/home', 'about')
const result4 = url.resolve('https://www.baidu.com/home/index', 'about')
const result5 = url.resolve('https://www.baidu.com/home/index?name=諸葛亮', 'about/hello')
console.log(result1)
console.log(result2)
console.log(result3)
console.log(result4)
console.log(result5)
輸出結(jié)果:

events模塊(事件驅(qū)動(dòng))
1.引入event模塊
2.創(chuàng)建一個(gè)eventEmitter實(shí)例
3.利用eventEmitter中的on方法和emit方法實(shí)現(xiàn)事件驅(qū)動(dòng),類似vue中的$on和$emit,即發(fā)布訂閱模式
可以解決異步需求,如下:
import fs from 'fs'
import event from 'events'
const eventEmitter = new event.EventEmitter()
eventEmitter.on('events', data => {
console.log('收到的數(shù)據(jù)', data.toString())
})
fs.readFile('static/index.html', (err, data) => {
eventEmitter.emit('events', data)
})
path模塊
import path from 'path'
// 獲取后綴名
const extName = path.extname('index.html') // .html
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
- 從零開始學(xué)習(xí)Node.js系列教程之設(shè)置HTTP頭的方法示例
- 從零開始學(xué)習(xí)Node.js系列教程之SQLite3和MongoDB用法分析
- 從零開始學(xué)習(xí)Node.js系列教程六:EventEmitter發(fā)送和接收事件的方法示例
- 從零開始學(xué)習(xí)Node.js系列教程五:服務(wù)器監(jiān)聽方法示例
- 從零開始學(xué)習(xí)Node.js系列教程四:多頁面實(shí)現(xiàn)數(shù)學(xué)運(yùn)算的client端和server端示例
- 從零開始學(xué)習(xí)Node.js系列教程之基于connect和express框架的多頁面實(shí)現(xiàn)數(shù)學(xué)運(yùn)算示例
- 從零開始學(xué)習(xí)Node.js系列教程四:多頁面實(shí)現(xiàn)的數(shù)學(xué)運(yùn)算示例
相關(guān)文章
npm?install編譯時(shí)報(bào)"Cannot?read?properties?of?null?(r
這篇文章主要給大家介紹了關(guān)于npm?install編譯時(shí)報(bào)“Cannot?read?properties?of?null?(reading?‘pickAlgorithm‘)“錯(cuò)誤的解決辦法,文中將解決方法介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
Node.js學(xué)習(xí)之內(nèi)置模塊fs用法示例
這篇文章主要介紹了Node.js學(xué)習(xí)之內(nèi)置模塊fs用法,結(jié)合實(shí)例形式詳細(xì)分析了node.js內(nèi)置模塊fs的基本功能、用法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-01-01
整理幾個(gè)關(guān)鍵節(jié)點(diǎn)深入理解nodejs
這篇文章主要介紹了整理幾個(gè)關(guān)鍵節(jié)點(diǎn)深入理解nodejs,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下,需要的小伙伴可以參考一下2022-07-07
使用Make構(gòu)建Node.js網(wǎng)站項(xiàng)目
這篇文章介紹了使用Make構(gòu)建Node.js網(wǎng)站項(xiàng)目的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01

