從零開始學(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地址,獲得一個被解析的url詳情對象,包含協(xié)議、域名、路徑、端口、查詢參數(shù)、哈希等信息。
第二個參數(shù)為boolean值,默認(rèn)為false,傳true會將query轉(zhuǎn)為對象
const parseUrl = url.parse('https://www.baidu.com/news?name=諸葛亮&age=18#helloworld', true) console.log(parseUrl)
2.format 方法
傳入一個url信息對象(即parse方法返回的對象),返回一個具體的路徑,該方法是parse方法的逆運用。
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 方法
拼接或替換次級路徑
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ū)動)
1.引入event模塊
2.創(chuàng)建一個eventEmitter實例
3.利用eventEmitter中的on方法和emit方法實現(xiàn)事件驅(qū)動,類似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系列教程四:多頁面實現(xiàn)數(shù)學(xué)運算的client端和server端示例
- 從零開始學(xué)習(xí)Node.js系列教程之基于connect和express框架的多頁面實現(xiàn)數(shù)學(xué)運算示例
- 從零開始學(xué)習(xí)Node.js系列教程四:多頁面實現(xiàn)的數(shù)學(xué)運算示例
相關(guān)文章
node使用Mongoose類庫實現(xiàn)簡單的增刪改查
Mongoose是在nodejs環(huán)境中對MongoDB數(shù)據(jù)庫操作的封裝,這篇文章主要介紹了node使用Mongoose類庫實現(xiàn)簡單的增刪改查,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11Node.js web 應(yīng)用如何封裝到Docker容器中
這篇文章主要介紹了Node.js web 應(yīng)用如何封裝到Docker容器中,幫助大家更好的學(xué)習(xí)node.js和使用docker容器,感興趣的朋友可以了解下2020-09-09NodeJS學(xué)習(xí)筆記之Connect中間件模塊(二)
本文續(xù)上文的內(nèi)容,介紹下nodejs中connect中間件的使用方式及用途,希望大家喜歡。2015-01-01