從零開始學(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)文章
node使用Mongoose類庫實(shí)現(xiàn)簡單的增刪改查
Mongoose是在nodejs環(huán)境中對(duì)MongoDB數(shù)據(jù)庫操作的封裝,這篇文章主要介紹了node使用Mongoose類庫實(shí)現(xiàn)簡單的增刪改查,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
Node.js web 應(yīng)用如何封裝到Docker容器中
這篇文章主要介紹了Node.js web 應(yīng)用如何封裝到Docker容器中,幫助大家更好的學(xué)習(xí)node.js和使用docker容器,感興趣的朋友可以了解下2020-09-09
nodejs 實(shí)現(xiàn)釘釘ISV接入的加密解密方法
這篇文章主要介紹了nodejs 實(shí)現(xiàn)釘釘ISV接入的加密解密方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧,需要的朋友可以參考下2017-01-01
node.js 使用ejs模板引擎時(shí)后綴換成.html
本文給大家分享一個(gè)nodejs的小技巧,將ejs模板引擎的模板后綴改成.html的使用方法,非常的簡單實(shí)用,這里推薦給大家。2015-04-04
NodeJS學(xué)習(xí)筆記之Connect中間件模塊(二)
本文續(xù)上文的內(nèi)容,介紹下nodejs中connect中間件的使用方式及用途,希望大家喜歡。2015-01-01

