nodejs基本模組(二)

2021-10-07 00:03:39 字數 1027 閱讀 9831

stream

1.含義:是nodejs提供在服務端可用的模組,目前支援這樣的額資料結構。流資料結構的特點:有序、依次讀入或者一次寫入、不能像array隨機定位。也叫標準輸入流(stdin)。

2.在node.js中,流也是乙個物件,我們只需要響應流的事件就可以了:data事件表示流的資料已經可以讀取了,end事件表示這個流已經到末尾了,沒有資料可以讀取了,error事件表示出錯了。

如下:

'use strick';

var fs = require ('fs');

var rs = fs.createreadstream('a.txt','utf-8');

rs.on('data',function(a));

rs.on('end',function())

rs.on('err',function(err))

所有可以讀取資料的流都繼承自stream.readable,所有可以寫入的流都繼承自stream.writable

pipe

定義:乙個readable流和乙個writable流串起來,所有的readable流進入writable流這種操作叫做pipe。

在node.js中readable流有乙個pipe()方法來進行將其串聯起來。這樣就可以將原始檔的資料寫入到目標檔案。

'use strick'

var fs = require('fs')

var rs = fs.createreadstream('a.txt')

var ws = fs.createwritestream('aout.txt')

rs.pipe(ws)

預設情況下,當readable流的資料讀取完畢時,end事件觸發後,將自動關閉writable流 ,如果不希望他關閉,需要傳入引數:

readable.pipe(writable,)

nodejs模組 http模組

處理url請求 var fs require fs 主頁function home res res.write content res.end about.html 關於頁面 function about res res.write content res.end 匯出頁面處理函式 exports....

nodejs 模組系統

模組系統分為原生系統跟檔案系統,他們的呼叫優先順序為 檔案系統快取區 原生系統 原生系統快取區 檔案系統 1.原生系統 http,path,fs 等 2.載入檔案系統時還可以指定自定義檔案 hello 或者 root node hello 路徑載入 3.mod 非原生系統的檔案系統 node.js ...

nodejs發布模組

nodejs安裝目錄 d soft nodejs 新建js檔案 例如 helloworld.js console.log hello world 1.建立乙個新的模組 d soft nodejs npm init生成乙個新的package.json 註冊所繫結的賬號 npm adduser 3.上傳...