NodeJs內(nèi)置模塊超詳細(xì)講解
一、fs文件系統(tǒng)模塊
1、fs.readFile()讀取文件
參數(shù)1(必):讀取文件的存放路徑
參數(shù)2(選):采用的編碼格式,一般默認(rèn)utf8
參數(shù)3(必):回調(diào)函數(shù),拿到讀取成功和失敗的結(jié)果
const fs = require('fs') fs.readFile('./hello.txt','utf8',function(err,dataStr) { // 如果讀取成功,則err值為null // 如果讀取失敗,則err值為錯(cuò)誤對(duì)象,dataStr值為undefined if(err) { return console.log('文件讀取失?。? + err.message) } console.log('文件讀取成功:' + dataStr) })
2、fs.writeFile()寫入文件
參數(shù)1(必):寫入文件的存放路徑
參數(shù)2(必):要寫入的內(nèi)容
參數(shù)3(選):采用的編碼格式,一般默認(rèn)utf8
參數(shù)4(必):回調(diào)函數(shù),拿到寫入成功和失敗的結(jié)果
const fs = require('fs') fs.readFile('./hello.txt','hello world',function(err) { // 如果寫入成功,則err值為null // 如果寫入失敗,則err值為錯(cuò)誤對(duì)象 if(err) { return console.log('文件寫入失?。? + err.message) } console.log('文件寫入成功') })
3、fs路徑問題
描述:在操作文件時(shí),如果提供的操作路徑是相對(duì)路徑,很容易出現(xiàn)路徑動(dòng)態(tài)拼接錯(cuò)誤問題
原因:代碼運(yùn)行的時(shí)候,會(huì)以執(zhí)行node命令時(shí)所處的目錄,動(dòng)態(tài)拼接出被 操作文件的完整路徑
解決方案:直接提供完整的路徑
// __dirname 表示當(dāng)前文件所處的目錄 fs.readFile(__dirname + '/hello.txt','utf8',function(err,dataStr) { })
二、path路徑模塊
1、path.join()拼接路徑
涉及到路徑拼接的操作,都要使用path.join()方法進(jìn)行處理,不要直接使用 + 進(jìn)行字符串的拼接,如果拼接的路徑有.
就會(huì)出問題,而path.join()可以處理掉這個(gè).
const path = require('path') const pathStr = path.join('/a','/b/c','../','/d','/e') console.log(pathStr) // \a\b\d\e
注意: ../
會(huì)抵消前面一層路徑
2、path.basename()獲取路徑中的文件名
參數(shù)1(必):路徑字符串
參數(shù)2(選):文件擴(kuò)展名
const path = require('path') const fpath = '/a/b/c/index.html' const fullName = path.basename(fpath) console.log(fullName); // index.html const nameWithoutExt = path.basename(fpath,'.html') console.log(nameWithoutExt ); // index
3、path.extname()獲取路徑中的文件擴(kuò)展名
參數(shù)(必):路徑字符串
const path = require('path') const fpath = '/a/b/c/index.html' const fext = path.extname(fpath) console.log(fext); // .html
三、http模塊
用來創(chuàng)建web服務(wù)器的模塊
1、創(chuàng)建最基本的web服務(wù)器
1、引入http核心模塊
2、創(chuàng)建Web服務(wù)器實(shí)例
3、為服務(wù)器綁定request事件,監(jiān)聽客戶端的請(qǐng)求
4、啟動(dòng)服務(wù)器
const http = require('http') const server = http.createServer() server.on('request',function(req,res) { console.log('Someone visit our web server.') }) server.listen(8080,function() { console.log('server running at http://127.0.0.1:8080') })
2、req請(qǐng)求對(duì)象
在事件處理函數(shù)中訪問與客戶端相關(guān)的數(shù)據(jù)或?qū)傩?/p>
server.on('request',(req) => { // req.url 是客戶端請(qǐng)求的 URL 地址 // req.method 是客戶端的 method 請(qǐng)求類型 })
3、res響應(yīng)對(duì)象
在事件處理函數(shù)中訪問與服務(wù)器相關(guān)的數(shù)據(jù)或?qū)傩?/p>
server.on('request',(req,res) => { const str = `您請(qǐng)求的URL地址是${req.url},請(qǐng)求的method類型為${req.method}` // 調(diào)用 res.setHeader()方法設(shè)置 Content-Type 響應(yīng)頭,解決中文亂碼的問題 res.setHeader('Content-Type','text/html;charset=utf-8') // res.end()方法,向客戶端響應(yīng)內(nèi)容 res.end(str) })
4、根據(jù)不同的url響應(yīng)不同的內(nèi)容
server.on('request',(req,res) => { const url = req.url let content = '<h1>404 Not Found</h1>' if(url === '/' || url === '/index.html') { content = '<h1>首頁</h1>' } else if(url === '/about.html') { content = '<h1>關(guān)于</h1>' } res.setHeader('Content-Type','text/html;charset=utf-8') res.end(content) })
到此這篇關(guān)于NodeJs內(nèi)置模塊超詳細(xì)講解的文章就介紹到這了,更多相關(guān)NodeJs內(nèi)置模塊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Node.js HTTP服務(wù)器中的文件、圖片上傳的方法
這篇文章主要介紹了Node.js HTTP服務(wù)器中的文件、圖片上傳的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

Dapr+NestJs編寫Pub及Sub裝飾器實(shí)戰(zhàn)示例

nodejs npm install全局安裝和本地安裝的區(qū)別

nodejs實(shí)現(xiàn)郵箱發(fā)送驗(yàn)證碼功能