nodejs插件有:“node-xlsx”、nodemailer、“node-readbility”、connect、“express-session”、“basic-auth”、bcryptjs、puppeteer、nightmare等。
本教程操作環(huán)境:windows7系統(tǒng)、nodejs 12.19.0版,DELL G3電腦。
nodejs插件
1、node-xlsx對(duì)Excel的讀與寫(xiě)
excel的導(dǎo)入導(dǎo)出是很多系統(tǒng)當(dāng)中都會(huì)出現(xiàn)的問(wèn)題,在NodeJS當(dāng)中,也不例外,現(xiàn)在,我們就通過(guò)NodeJS來(lái)實(shí)現(xiàn)Excel文件的讀寫(xiě)
NodeJS里面,實(shí)現(xiàn)文件的讀與寫(xiě),我們使用的是第三方的工具node-xlsx這個(gè)模塊,這個(gè)模塊同時(shí)支持2003的Excel(.xls)格式與支持2007的Excel的格式(.xlsx)
現(xiàn)在,我們就看一下這個(gè)模塊的具體操作
對(duì)Excel的讀取操作
首先,我們需要安裝這個(gè)模塊
cnpm install node-xlsx --save
第二步,導(dǎo)入該模塊,讀取Excel文件
const xlsx=require('node-xlsx');const DBUtil=require('./utils/DBUtil.js');const fs=require('fs');const path=require('path');function readExcel(path){ var excel=xlsx.parse(path); return excel;}var obj=readExcel(path.join(__dirname,"./files/studentinfo.xls"));console.log(obj[0].data);
上面的代碼就已經(jīng)完成了對(duì)Excel文件的讀取操作,這個(gè)時(shí)候我們讀出來(lái)的是一個(gè)對(duì)象,可以在控制臺(tái)看到其中的信息
對(duì)Excel的寫(xiě)入操作
現(xiàn)在,我們就演示將數(shù)據(jù)庫(kù)的一個(gè)張表的信息讀取出來(lái),然后保存到本地電腦當(dāng)中,代碼如下
const excel=require('node-xlsx');const fs=require('fs');const path=require('path');const DBUtil=require('./utils/DBUtil.js');function writeExcel(){ var conn=DBUtil.getConn(); conn.query("select * from studentinfo",[],(err,result)=>{ if(err){ } else{ var excelArr=[]; var headerRow=[]; for(var i in result[0]){ headerRow.push(i); } excelArr.push(headerRow); for(var i=0;i<result.length;i++){ var temp=[]; for(var j=0;j<headerRow.length;j++){ temp.push(result[i][headerRow[j]]); } excelArr.push(temp); } try { var buff=excel.build([{name:'學(xué)生信息',data:excelArr}]); fs.writeFileSync(path.join(__dirname,"./files/01.xlsx"),buff); console.log("ok"); } catch (error) { console.log(err); } } }); conn.end();}writeExcel();
在這里,我們發(fā)現(xiàn),寫(xiě)入excel稍微麻煩一點(diǎn),因?yàn)樵谶@里,我們需要將數(shù)據(jù)庫(kù)中得到的結(jié)果進(jìn)行重新的組合,再生成Excel
思考:如果在Express框架當(dāng)中,生成好的Excel文件如下讓用戶(hù)去下載,保存到本地?
2、nodemailer對(duì)郵件的發(fā)送
nodejs對(duì)發(fā)送郵件給用戶(hù),使用的場(chǎng)景也非常多,例如,我們經(jīng)常會(huì)看到一個(gè)用戶(hù)注冊(cè)以后,就會(huì)發(fā)送一封注冊(cè)信息到用戶(hù)所注冊(cè)的郵箱當(dāng)中去, 這時(shí)候,如果我們要完成這一個(gè)功能,就需要使用一個(gè)nodemailer的第三方模塊,具體使用步驟如下:
安裝相應(yīng)模塊
$ cnpm install nodemailer --asve $ yarn add nodemailer
導(dǎo)入模塊,完成代碼
const nodemailer=require('nodemailer');var transport = nodemailer.createTransport({ service:"qq", auth:{ user:"365055754@qq.com", pass:"peshapwpokgvcahe" }});var options={ from:"365055754@qq.com", to:"lovesnsfi@live.com", subject:"這是一封來(lái)自nodemailer發(fā)送的郵件信息", text:"這是一封來(lái)自nodemailer發(fā)送的郵件信息"+(new Date()).toLocaleString(), html:"<h2>這是一封來(lái)自<u>nodemail</u>的測(cè)試郵件···</h2>"};transport.sendMail(options,(err,info)=>{ if(err){ console.log(err); } else{ console.log(info); }});
發(fā)送成功以后的信息
{ accepted: [ 'lovesnsfi@live.com' ], rejected: [], envelopeTime: 221, messageTime: 830, messageSize: 801, response: '250 Ok: queued as ', envelope: { from: '365055754@qq.com', to: [ 'lovesnsfi@live.com' ] }, messageId: '<2fbef9f1-1041-fd59-1111-0b987e8d81da@qq.com>' }
這個(gè)時(shí)候,只要程序能夠在這u個(gè)地方完成我們的發(fā)送請(qǐng)求,就會(huì)返回上面的信息,如果沒(méi)有看到上面的信息,我們就需要在這個(gè)地方去查看一下error返回的信息
說(shuō)明:在此得配置發(fā)送服務(wù)器的時(shí)候,我們可以使用第三方的服務(wù)器,也可以使用內(nèi)置的服務(wù)器
思考:如果將發(fā)送的內(nèi)容用模板去完成替換
const fs=require('fs');const path=require('path');class MailTemplateModel{ constructor(userName,u_id,registerTime,mail){ this.userName=userName; this.u_id=u_id; this.registerTime=registerTime; this.mail=mail; } toString(){ var str=`尊敬的${this.userName}你好! 歡迎注冊(cè)成為我們的會(huì)員,你的賬號(hào)為${this.u_id},你的注冊(cè)時(shí)間為:${this.registerTime}。 請(qǐng)注意保管好您的賬號(hào)與密碼,如有問(wèn)題歡迎發(fā)送郵件至${this.mail}! 謝謝!祝您生活愉快!`; return str; }}module.exports=MailTemplateModel;
上面的代碼就是將要發(fā)送的郵件內(nèi)容封裝成了一個(gè)對(duì)象,然后使用模板語(yǔ)法進(jìn)行了拼接字符串
思考:上在的郵件發(fā)送內(nèi)容,我們寫(xiě)在外部單獨(dú)的txt文件當(dāng)中,然后通過(guò)String對(duì)象的replace來(lái)進(jìn)行實(shí)現(xiàn),這個(gè)功能怎么實(shí)現(xiàn)?
3、child_process
可以創(chuàng)建子進(jìn)程,執(zhí)行shell腳本。
4、node-readbility
一個(gè)可以將網(wǎng)站內(nèi)容化為簡(jiǎn)單內(nèi)容的插件。
5、connect
其實(shí)express也使用這個(gè)插件,使用connect也可以寫(xiě)web程序。
6、express-session
這是一個(gè)使用會(huì)話(huà)的插件,默認(rèn)是永遠(yuǎn),和tomcat30分鐘不同,所以需要自己設(shè)置超時(shí)時(shí)間。
7、basic-auth插件
用于最簡(jiǎn)單的認(rèn)證方式,一般用在api請(qǐng)求上。
8、bcryptjs插件(bcrypt安裝過(guò)程中總報(bào)錯(cuò))
用來(lái)使用加鹽的方式進(jìn)行hash處理。
9、爬蟲(chóng)類(lèi)集錦:
(1)靜態(tài)頁(yè)面和api數(shù)據(jù)的爬取:request+cheerio/jsdom,request是一個(gè)請(qǐng)求庫(kù),可以請(qǐng)求post、get信息,獲取html數(shù)據(jù)后,使用第三方的解析庫(kù)解析,cheerio就可以,對(duì)于js動(dòng)態(tài)渲染頁(yè)面可以考慮使用jsdom,但是很可惜,這個(gè)是同步的,而且畢竟不是瀏覽器。
(2)動(dòng)態(tài)渲染的頁(yè)面的爬取
puppeteer:使用chromiun瀏覽器,異步請(qǐng)求,效率很高,而且開(kāi)放了很多操作瀏覽器的api,很方便。
nightmare:api使用非常方便的,使用electron中的瀏覽器,雖然沒(méi)有使用過(guò),但是感覺(jué)這個(gè)沒(méi)有puppeteer靈活。
jsdom:同步已經(jīng)讓我放棄了它的使用。和selenium一樣。
10、moment.js
這是一個(gè)輕量級(jí)格式解析庫(kù),如果自己寫(xiě)格式解析函數(shù),需要幾個(gè)十幾行的函數(shù)代碼,用這個(gè)很方便。