NodeJS: https://github.com/nodejs/node/ 在github中想要查找某個(gè)文件,按t;
例如: 在使用fs.createWriteStream時(shí),文檔中沒(méi)有具體方法,就可以通過(guò)查看源碼,通過(guò)源碼找對(duì)應(yīng)的構(gòu)造函數(shù)看有哪些屬性設(shè)置,查看原型有哪些方法可用;
npm中管理的包
npm ls --depth 0 // 即依賴子集為0
$ npm ls --depth 1 // 即依賴子集的第一層
nodemon:監(jiān)視您的node.js應(yīng)用程序的任何更改并自動(dòng)重新啟動(dòng)服務(wù)器
nodemon xxx.js // 運(yùn)行對(duì)應(yīng)文件即可
一、文件監(jiān)視
- fs.watchFile(filename[, options], listener(curr,prev))
// { persistent: true, interval: 5007 },即表示持續(xù)不斷監(jiān)視,時(shí)間間隔為5007毫秒
options:{persistent,interval}
const target = path.join(__dirname, process.argv[2]);
fs.watchFile(target, (curr, prev)=>{
console.log(`curr:${curr.size}; prev: ${prev.size}`);
});
fs.watch(filename[,options][,listener])
利用文件監(jiān)視實(shí)現(xiàn)自動(dòng) markdown 文件轉(zhuǎn)換
相關(guān)鏈接:
Markdown轉(zhuǎn)換: https://github.com/chjj/marked
同步瀏覽器端: https://github.com/Browsersync/browser-sync
思路:
- 利用fs模塊的文件監(jiān)視功能監(jiān)視指定MD文件
- 當(dāng)文件發(fā)生變化后,借助marked包提供的markdown to html功能將改變后的MD文件轉(zhuǎn)換為HTML
- 將得到的HTML替換到模版中(html = template.replace('{{content}}', html).replace('{{style}}', css);)
- 利用BrowserSync模塊實(shí)現(xiàn)瀏覽器自動(dòng)刷新
markdown的樣式有很多可以選擇,選取的不同,html結(jié)構(gòu)也會(huì)有所不同。
關(guān)于github.css主體內(nèi)容是包裹在div為.vs中:
<body>
<div class='vs'>{{content}}</div>
</body>
BrowserSync模塊的使用
1、$ npm install browser-sync // 安裝browser-sync
2、const browserSync = require("browser-sync"); // 導(dǎo)入模塊
3、通過(guò)browserSync創(chuàng)建一個(gè)文件服務(wù)器
browserSync({
server: path.dirname(target), // 文件服務(wù)器的目錄
index:indexName // 在開(kāi)啟服務(wù)器之后會(huì)打開(kāi)頁(yè)面,該頁(yè)面作為網(wǎng)站根目錄
});
4、當(dāng)文件修改后,刷新瀏覽器頁(yè)面
browserSync.reload(indexName); // fileName是完整路徑
添加完BrowserSync后,就會(huì)在對(duì)應(yīng)目錄創(chuàng)建并啟動(dòng)一個(gè)服務(wù)器,自動(dòng)打開(kāi)頁(yè)面(或手動(dòng)打開(kāi),命令行有提示)http://192.168.1.112:3000 此時(shí)是在根目錄下,也可以具體訪問(wèn)某個(gè)文件http://192.168.1.112:3000/xxx.html
二、文件流
- 什么是流?
- 流是程序輸入或輸出的一個(gè)連續(xù)的字節(jié)序列
- 文件流、網(wǎng)絡(luò)流
- 設(shè)備(例如鼠標(biāo)、鍵盤、磁盤、屏幕、調(diào)制解調(diào)器和打印機(jī))的輸入和輸出都是用流來(lái)處理的
- 文件流
- 文件流就是以面向?qū)ο蟮母拍顚?duì)文件數(shù)據(jù)進(jìn)行的抽象
- 文件流定義了一些對(duì)文件數(shù)據(jù)的操作方式
- Node中的流操作
- fs.createReadStream() 可讀流
讀取流常用 API
Event:data、end、error
Method: read([size])、pause()、isPause()、resume()、
setEncoding(encoding)、pipe(destination[, options])、unpipe([destination])
- fs.createWriteStream() 可寫(xiě)流
Event: error、pipe
Method:write(chunk[, encoding][, callback])、end([chunk][, encoding][, callback])、setDefaultEncoding(encoding)
案例: 拷貝文件功能
fs-extra文件操作模塊 可以是拷貝、刪除、創(chuàng)建等操作
三、網(wǎng)絡(luò)操作
- NET模塊
在Node中socket通信的一種實(shí)現(xiàn)
let server = net.createServer( (socket) => { // 當(dāng)客戶端鏈接時(shí),即會(huì)觸發(fā)
// client有三個(gè)參數(shù): 端口號(hào)、IPV4/IPV6、ip
var client = socket.address();
// 或直接通過(guò)socket.remoteAddress / socket.remotePort 訪問(wèn)到
// 監(jiān)聽(tīng)socket,網(wǎng)絡(luò)流
socket.on('data', (chunk)=>{
});
} );
// 綁定IP, 監(jiān)聽(tīng)2017端口
const port = 2017;
// server.listen(options[, callback])
// options { host: '192.168.1.100', port: 80}
server.listen(port, (err) => {
});
// 為了方便操作,命令行方式鏈接服務(wù)端(使用telnet操作): $ telnet 192.168.1.100 2017
// 簡(jiǎn)便寫(xiě)法
// port 端口號(hào), host IP地址
// net.createServer(socketConneted).listen(port, host, (err) => { });
net.createServer(socketConneted).listen({port:port, host: host}, (err) => { });
function socketConneted(socket){ ... }
// 創(chuàng)建鏈接
const client = net.connect({port: '2017', host:'192.168.1.100'}, ()=>{
console.log('鏈接服務(wù)器成功');
client.write('hello ??');
});
// 監(jiān)聽(tīng)到數(shù)據(jù)
client.on('data', (chunk) => {
console.log('接收到數(shù)據(jù):' + chunk.toString());
})

process.stdin.on('data', (chunk) => {
// 轉(zhuǎn)成string類型,并且去掉前后空白
console.log(chunk.toString().trim());
});
案例:簡(jiǎn)易的聊天室
查看文檔是net(http和http是高度封裝的,net中更多了解socket通信是怎么的一個(gè)過(guò)程,如何的一個(gè)操作)
作者:西門奄
鏈接:http://www.itdecent.cn/u/77035eb804c3
來(lái)源:簡(jiǎn)書(shū)
簡(jiǎn)書(shū)著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請(qǐng)聯(lián)系作者獲得授權(quán)并注明出處。