概念:Javascript運行時平臺,不是語言,也不是框架,是一個平臺。javascript
Hello.jscss
1.編寫js文件html
2.定位到目錄vue
3.輸入node xxx.js,解釋執行java
let str="hello world"console.log(str)console.log(window) //沒有bomconsole.log(document) //沒有dom12345
多出來的功能:node
//1.定義導入包 let fs=require('fs') //2.讀取hello.js //參數:路徑,匿名函數 fs.readFile('hello.js',function (error,data) { console.log(data);//utf-8編碼 console.log(data.toString()); //格式能夠輸出 })12345678
輸出的格式不是ASCI編碼格式,注意!!!python
關於參數的代碼理解android
//1.定義導入包let fs=require('fs')//2.讀取hello.js//參數:路徑,匿名函數fs.readFile('hello2.js',function (error,data) { // if(error==null) // console.log('沒有錯誤') if(error) console.log('讀取文件失敗!') else console.log(data.toString());})123456789101112
//1.導入http包let http=require('http')//2.定義server變量,返回一個server實例;let server=http.createServer()//3.服務器的響應,接受request請求,產生會回調事件server.on('request',function () { console.log('接受來自客戶端的請求....')})//4.綁定到響應的端口號;server.listen(3000,function () { console.log('服務器啓動了,能夠經過http://localhost:3000/來訪問...')})123456789101112
//1.導入http包let http=require('http')//2.定義server變量,返回一個server實例;let server=http.createServer()//3.服務器的響應,接受request請求,產生會回調事件//增長參數:request、responseserver.on('request',function (request,response) { console.log('接受來自客戶端的請求....') console.log('請求路徑是:'+request.url) //response:響應輸出;服務器發送給客戶端 response.write('你好') response.write('test...aaa') response.end()})//4.綁定到響應的端口號;server.listen(3000,function () { console.log('服務器啓動了,能夠經過http://localhost:3000/來訪問...')})123456789101112131415161718
根據不一樣的url返回不一樣的服務git
let url=request.urlgithub
url返回的是/以後的路徑
//1.導入http包let http=require('http')//2.定義server變量,返回一個server實例;let server=http.createServer()//3.服務器的響應,接受request請求,產生會回調事件//增長參數:request、responseserver.on('request',function (request,response) { console.log('接受來自客戶端的請求....') console.log('請求路徑是:'+request.url) //3.1獲取請求的url路徑信息; let url=request.url console.log('----'+url) //接下來,根據url進行判斷; if(url=='/'){ response.end('index.html') }else if(url=='/login'){ response.end('login.html') }else if(url=='/products'){ let products=[ {name:'蘋果',price:4.5}, {name:'香蕉',price:3.5}, {name:'橘子',price:2.5}] response.end(JSON.stringify(products)) } else{ response.end('404 Error') } //response:響應輸出;服務器發送給客戶端 // response.write('你好') // response.write('test...aaa') // response.end()})//4.綁定到響應的端口號;server.listen(3000,function () { console.log('服務器啓動了,能夠經過http://localhost:3000/來訪問...')})123456789101112131415161718192021222324252627282930313233343536
模塊分爲三種:
以前代碼require(‘fs’)就是引用的內置模塊。Node爲JS提供了不少服務器級別的API,這些API絕大多數都被包裝到了一個有名字的核心模塊。例如文件操做的fs模塊。
使用格式:
let 變量名=require(‘模塊名’) //fs:文件讀寫模塊
let http=require(‘http’) //http:協議模塊;
let path=require(‘path’) //路徑處理模塊
let os=require(‘os’)
let fs=require(‘fs’)
let fs = require("fs");//建立文件夾//fs.mkdir('./hello',(err,data)=>{// console.log(err);// console.log(data);//});//修改文件夾//fs.rename('./hello','./testyyh',(err)=>{// if(err){// console.log("error");// }else{// console.log("ok");// }//});//刪除文件夾-->只能刪除空文件夾fs.rmdir('./testyyh',(err)=>{ if(err){ console.log('刪除失敗'); console.log(err); }else{ console.log('ok'); }1234567891011121314151617181920212223
const fs = require('fs');//建立文件,覆蓋寫入,正常的話則是追加//fs.writeFile('test.txt','這是個人第一個文件',(err)=>{// if(err){// console.log('建立失敗');// console.log(err);// }else{// // console.log('建立成功!');// }// //});//讀取文件,在原來的基礎上追加文件//fs.appendFile('test.txt','韓梅梅你好',(err)=>{// console.log(err);//});//讀取文件這是第一種方式//fs.readFile('test.txt',(err,msg)=>{// console.log(err);// console.log(msg.toString("utf8"));//在轉化爲字符串的時候指定編碼方式//});//在參數中指定編碼方式//fs.readFile('test.txt','utf8',(err,msg)=>{// // console.log(err);// console.log(msg);//});//刪除文件//fs.unlink('./test.txt',(err)=>{// console.log(err);//});1234567891011121314151617181920212223242526272829303132
const fs = require('fs');fs.readdir('./',(err,dirs)=>{ for(let index=0;index<dirs.length;index++){ console.log(dirs[index]); }});fs.stat('./',(err,stats)=>{ if(stats.isFile()){ console.log('是一個文件'); }else{ console.log('是一個目錄'); }});1234567891011121314
詳細介紹自定義模塊:
建立一個模塊(一個js文件就是一個模塊),這是test.js
let hellomodule={ sayhell(){ console.log("hello world"); }}module.exports=hellomodule123456
在另一個文件進行調用
//1.導入文件;必須是./的模式,直接路徑不可用;let testModule=require('./module.js')//2.直接使用;console.log(testModule)testModule.sayhell()12345
//1.引用os核心模塊let os=require('os')//2.輸出cpu的信息;console.log(os.cpus())//內存信息console.log(os.totalmem())console.log(os.type)//.1.1引用path核心模塊let path=require('path')//2.測試path;path.extname('c:/soft/oop.java')//ext:擴展名console.log(path.extname('c:/soft/oop.java'))123456789101112131415
//輸出端口號;console.log('請求端口號:'+request.socket.remotePort)console.log('請求地址:'+request.socket.remoteAddress)123
let http=require('http')let server=http.createServer()server.on('request',(req,resp)=>{ console.log('請求地址:'+req.url) console.log('請求客戶端地址信息:'+req.socket.remotePort,req.socket.remoteAddress) resp.end('hello world.張晨光...')})server.listen(5000,()=>{ console.log('服務器啓動能夠訪問了...')})1234567891011121314
能夠啓動多個服務,端口號不一樣就行!!!
let http=require('http')let fs=require('fs')let server=http.createServer()server.on('request',(req,resp)=>{ let url=req.url if(url==='/'){ fs.readFile('./hello.html',(err,data)=>{ if(err){ resp.setHeader('Content-Type','text/plain;charset=utf-8') resp.end('文件讀取失敗!') }else{ resp.setHeader('Content-Type','text/html;charset=utf-8') resp.end(data) } }) }})server.listen(3000,()=>{ console.log('服務器啓動能夠訪問了...')})1234567891011121314151617181920212223
能夠讀取圖片的程序升級
let http=require('http')let fs=require('fs')let server=http.createServer()server.on('request',(req,resp)=>{ let url=req.url if(url==='/'){ fs.readFile('./hello.html',(err,data)=>{ if(err){ resp.setHeader('Content-Type','text/plain;charset=utf-8') resp.end('文件讀取失敗!') }else{ resp.setHeader('Content-Type','text/html;charset=utf-8') resp.end(data) } }) } else if(url==='/photo'){ fs.readFile('./resources/2a.jpg',(err,data)=>{ if(err){ resp.setHeader('Content-Type','text/plain;charset=utf-8') resp.end('圖片讀取失敗!') }else{ resp.setHeader('Content-Type','image/jpeg') resp.end(data) } }) }})server.listen(3000,()=>{ console.log('服務器啓動能夠訪問了...')})12345678910111213141516171819202122232425262728293031323334
Http Mime Type
文件擴展名 | Content-Type(Mime-Type) | 文件擴展名 | Content-Type(Mime-Type) |
---|---|---|---|
.*( 二進制流,不知道下載文件類型) | application/octet-stream | .tif | image/tiff |
.001 | application/x-001 | .301 | application/x-301 |
.323 | text/h323 | .906 | application/x-906 |
.907 | drawing/907 | .a11 | application/x-a11 |
.acp | audio/x-mei-aac | .ai | application/postscript |
.aif | audio/aiff | .aifc | audio/aiff |
.aiff | audio/aiff | .anv | application/x-anv |
.asa | text/asa | .asf | video/x-ms-asf |
.asp | text/asp | .asx | video/x-ms-asf |
.au | audio/basic | .avi | video/avi |
.awf | application/vnd.adobe.workflow | .biz | text/xml |
.bmp | application/x-bmp | .bot | application/x-bot |
.c4t | application/x-c4t | .c90 | application/x-c90 |
.cal | application/x-cals | .cat | application/vnd.ms-pki.seccat |
.cdf | application/x-netcdf | .cdr | application/x-cdr |
.cel | application/x-cel | .cer | application/x-x509-ca-cert |
.cg4 | application/x-g4 | .cgm | application/x-cgm |
.cit | application/x-cit | .class | java/* |
.cml | text/xml | .cmp | application/x-cmp |
.cmx | application/x-cmx | .cot | application/x-cot |
.crl | application/pkix-crl | .crt | application/x-x509-ca-cert |
.csi | application/x-csi | .css | text/css |
.cut | application/x-cut | .dbf | application/x-dbf |
.dbm | application/x-dbm | .dbx | application/x-dbx |
.dcd | text/xml | .dcx | application/x-dcx |
.der | application/x-x509-ca-cert | .dgn | application/x-dgn |
.dib | application/x-dib | .dll | application/x-msdownload |
.doc | application/msword | .dot | application/msword |
.drw | application/x-drw | .dtd | text/xml |
.dwf | Model/vnd.dwf | .dwf | application/x-dwf |
.dwg | application/x-dwg | .dxb | application/x-dxb |
.dxf | application/x-dxf | .edn | application/vnd.adobe.edn |
.emf | application/x-emf | .eml | message/rfc822 |
.ent | text/xml | .epi | application/x-epi |
.eps | application/x-ps | .eps | application/postscript |
.etd | application/x-ebx | .exe | application/x-msdownload |
.fax | image/fax | .fdf | application/vnd.fdf |
.fif | application/fractals | .fo | text/xml |
.frm | application/x-frm | .g4 | application/x-g4 |
.gbr | application/x-gbr | . | application/x- |
.gif | image/gif | .gl2 | application/x-gl2 |
.gp4 | application/x-gp4 | .hgl | application/x-hgl |
.hmr | application/x-hmr | .hpg | application/x-hpgl |
.hpl | application/x-hpl | .hqx | application/mac-binhex40 |
.hrf | application/x-hrf | .hta | application/hta |
.htc | text/x-component | .htm | text/html |
.html | text/html | .htt | text/webviewhtml |
.htx | text/html | .icb | application/x-icb |
.ico | image/x-icon | .ico | application/x-ico |
.iff | application/x-iff | .ig4 | application/x-g4 |
.igs | application/x-igs | .iii | application/x-iphone |
.img | application/x-img | .ins | application/x-internet-signup |
.isp | application/x-internet-signup | .IVF | video/x-ivf |
.java | java/* | .jfif | image/jpeg |
.jpe | image/jpeg | .jpe | application/x-jpe |
.jpeg | image/jpeg | .jpg | image/jpeg |
.jpg | application/x-jpg | .js | application/x-javascript |
.jsp | text/html | .la1 | audio/x-liquid-file |
.lar | application/x-laplayer-reg | .latex | application/x-latex |
.lavs | audio/x-liquid-secure | .lbm | application/x-lbm |
.lmsff | audio/x-la-lms | .ls | application/x-javascript |
.ltr | application/x-ltr | .m1v | video/x-mpeg |
.m2v | video/x-mpeg | .m3u | audio/mpegurl |
.m4e | video/mpeg4 | .mac | application/x-mac |
.man | application/x-troff-man | .math | text/xml |
.mdb | application/msaccess | .mdb | application/x-mdb |
.mfp | application/x-shockwave-flash | .mht | message/rfc822 |
.mhtml | message/rfc822 | .mi | application/x-mi |
.mid | audio/mid | .midi | audio/mid |
.mil | application/x-mil | .mml | text/xml |
.mnd | audio/x-musicnet-download | .mns | audio/x-musicnet-stream |
.mocha | application/x-javascript | .movie | video/x-sgi-movie |
.mp1 | audio/mp1 | .mp2 | audio/mp2 |
.mp2v | video/mpeg | .mp3 | audio/mp3 |
.mp4 | video/mpeg4 | .mpa | video/x-mpg |
.mpd | application/vnd.ms-project | .mpe | video/x-mpeg |
.mpeg | video/mpg | .mpg | video/mpg |
.mpga | audio/rn-mpeg | .mpp | application/vnd.ms-project |
.mps | video/x-mpeg | .mpt | application/vnd.ms-project |
.mpv | video/mpg | .mpv2 | video/mpeg |
.mpw | application/vnd.ms-project | .mpx | application/vnd.ms-project |
.mtx | text/xml | .mxp | application/x-mmxp |
.net | image/pnetvue | .nrf | application/x-nrf |
.nws | message/rfc822 | .odc | text/x-ms-odc |
.out | application/x-out | .p10 | application/pkcs10 |
.p12 | application/x-pkcs12 | .p7b | application/x-pkcs7-certificates |
.p7c | application/pkcs7-mime | .p7m | application/pkcs7-mime |
.p7r | application/x-pkcs7-certreqresp | .p7s | application/pkcs7-signature |
.pc5 | application/x-pc5 | .pci | application/x-pci |
.pcl | application/x-pcl | .pcx | application/x-pcx |
application/pdf | application/pdf | ||
.pdx | application/vnd.adobe.pdx | .pfx | application/x-pkcs12 |
.pgl | application/x-pgl | .pic | application/x-pic |
.pko | application/vnd.ms-pki.pko | .pl | application/x-perl |
.plg | text/html | .pls | audio/scpls |
.plt | application/x-plt | .png | image/png |
.png | application/x-png | .pot | application/vnd.ms-powerpoint |
.ppa | application/vnd.ms-powerpoint | .ppm | application/x-ppm |
.pps | application/vnd.ms-powerpoint | .ppt | application/vnd.ms-powerpoint |
.ppt | application/x-ppt | .pr | application/x-pr |
.prf | application/pics-rules | .prn | application/x-prn |
.prt | application/x-prt | .ps | application/x-ps |
.ps | application/postscript | .ptn | application/x-ptn |
.pwz | application/vnd.ms-powerpoint | .r3t | text/vnd.rn-realtext3d |
.ra | audio/vnd.rn-realaudio | .ram | audio/x-pn-realaudio |
.ras | application/x-ras | .rat | application/rat-file |
.rdf | text/xml | .rec | application/vnd.rn-recording |
.red | application/x-red | .rgb | application/x-rgb |
.rjs | application/vnd.rn-realsystem-rjs | .rjt | application/vnd.rn-realsystem-rjt |
.rlc | application/x-rlc | .rle | application/x-rle |
.rm | application/vnd.rn-realmedia | .rmf | application/vnd.adobe.rmf |
.rmi | audio/mid | .rmj | application/vnd.rn-realsystem-rmj |
.rmm | audio/x-pn-realaudio | .rmp | application/vnd.rn-rn_music_package |
.rms | application/vnd.rn-realmedia-secure | .rmvb | application/vnd.rn-realmedia-vbr |
.rmx | application/vnd.rn-realsystem-rmx | .rnx | application/vnd.rn-realplayer |
.rp | image/vnd.rn-realpix | .rpm | audio/x-pn-realaudio-plugin |
.rsml | application/vnd.rn-rsml | .rt | text/vnd.rn-realtext |
.rtf | application/msword | .rtf | application/x-rtf |
.rv | video/vnd.rn-realvideo | .sam | application/x-sam |
.sat | application/x-sat | .sdp | application/sdp |
.sdw | application/x-sdw | .sit | application/x-stuffit |
.slb | application/x-slb | .sld | application/x-sld |
.slk | drawing/x-slk | .smi | application/smil |
.smil | application/smil | .smk | application/x-smk |
.snd | audio/basic | .sol | text/plain |
.sor | text/plain | .spc | application/x-pkcs7-certificates |
.spl | application/futuresplash | .spp | text/xml |
.ssm | application/streamingmedia | .sst | application/vnd.ms-pki.certstore |
.stl | application/vnd.ms-pki.stl | .stm | text/html |
.sty | application/x-sty | .svg | text/xml |
.swf | application/x-shockwave-flash | .tdf | application/x-tdf |
.tg4 | application/x-tg4 | .tga | application/x-tga |
.tif | image/tiff | .tif | application/x-tif |
.tiff | image/tiff | .tld | text/xml |
.top | drawing/x-top | .torrent | application/x-bittorrent |
.tsd | text/xml | .txt | text/plain |
.uin | application/x-icq | .uls | text/iuls |
.vcf | text/x-vcard | .vda | application/x-vda |
.vdx | application/vnd.visio | .vml | text/xml |
.vpg | application/x-vpeg005 | .vsd | application/vnd.visio |
.vsd | application/x-vsd | .vss | application/vnd.visio |
.vst | application/vnd.visio | .vst | application/x-vst |
.vsw | application/vnd.visio | .vsx | application/vnd.visio |
.vtx | application/vnd.visio | .vxml | text/xml |
.wav | audio/wav | .wax | audio/x-ms-wax |
.wb1 | application/x-wb1 | .wb2 | application/x-wb2 |
.wb3 | application/x-wb3 | .wbmp | image/vnd.wap.wbmp |
.wiz | application/msword | .wk3 | application/x-wk3 |
.wk4 | application/x-wk4 | .wkq | application/x-wkq |
.wks | application/x-wks | .wm | video/x-ms-wm |
.wma | audio/x-ms-wma | .wmd | application/x-ms-wmd |
.wmf | application/x-wmf | .wml | text/vnd.wap.wml |
.wmv | video/x-ms-wmv | .wmx | video/x-ms-wmx |
.wmz | application/x-ms-wmz | .wp6 | application/x-wp6 |
.wpd | application/x-wpd | .wpg | application/x-wpg |
.wpl | application/vnd.ms-wpl | .wq1 | application/x-wq1 |
.wr1 | application/x-wr1 | .wri | application/x-wri |
.wrk | application/x-wrk | .ws | application/x-ws |
.ws2 | application/x-ws | .wsc | text/scriptlet |
.wsdl | text/xml | .wvx | video/x-ms-wvx |
.xdp | application/vnd.adobe.xdp | .xdr | text/xml |
.xfd | application/vnd.adobe.xfd | .xfdf | application/vnd.adobe.xfdf |
.xhtml | text/html | .xls | application/vnd.ms-excel |
.xls | application/x-xls | .xlw | application/x-xlw |
.xml | text/xml | .xpl | audio/scpls |
.xq | text/xml | .xql | text/xml |
.xquery | text/xml | .xsd | text/xml |
.xsl | text/xml | .xslt | text/xml |
.xwd | application/x-xwd | .x_b | application/x-x_b |
.sis | application/vnd.symbian.install | .sisx | application/vnd.symbian.install |
.x_t | application/x-x_t | .ipa | application/vnd.iphone |
.apk | application/vnd.android.package-archive | .xap | application/x-silverlight-app |
art-template
vue
必須經過npm 安裝
文件做用域
通訊規則
模塊做用域
使用require來加載模塊
語法:var|let 變量名=require(‘模塊’)
做用:執行被加載模塊中的代碼;獲得被加載模塊中的exports導出接口對象
使用exports接口對象來導出模塊中的成員
node是模塊做用域,全部成員再當前模塊有效;
module.exports=‘字符串’
module.exports=function(){}
後面會覆蓋前面的值;
注意:exports是module.exports的一個引用,不能直接賦值。
//路徑形式的模塊// ./// ../// /xxx 通常不用// / 標識當前模塊系統所屬的磁盤根路徑// .js 後綴名能夠省略// require(./foo.js)//核心模塊實際上也是文件,核心模塊文件已經編譯到node.exe了//github.com-->node查看源碼能夠看到fs.js os.js//https://github.com/nodejs/node//https://github.com/nodejs/node/tree/master/lib//第三方模塊:本質上也是文件,須要經過npm下載,使用的時候仍然經過require('包名')來加載使用//不可能由任何一個第三方模塊和核心模塊文件名一致//既不是核心模塊,也不是核心路徑的模塊,先找到node-modules目錄,其次找其中的art-template// node_modules/art-template// node_modules/art-template/package.json文件// node_modules/art-template/package.json中的main屬性// main屬性中記錄了art-template的入口模塊,加載第三方包(包名),實際最終加載的仍是文件// 模擬實現// let template=require('art-template')// 模擬一下require('a')123456789101112131415161718192021222324252627
測試第三方模塊,在node_modules下新建一個a目錄,建立package.json的文件
{ "main":"foo.js"}123
foo.js
console.log('a中的foo.js被加載了...')module.exports=()=>{ console.log('fooooooo....')}12345
//若是package.json不存在或者main指定的入口模塊也沒有,則node會自動查找該目錄下的index.js//即index.js會做爲一個默認備選項。若是上述條件不成立,則去上級目錄去尋找node_modules的目錄,沒有繼續往//上級尋找,在a目錄下建立index.jsconsole.log('a--->index')1234
若是繼續往上一級查找node_modules,尚未找到,則顯示:
Error: Cannot find module ‘vue’
注意:咱們的一個項目有且只有一個node_modules,這樣項目均可以加載到第三方模塊包,不會出現多個。
apache軟件默認有一個www目錄,此目錄下的html文件均可以經過node。案例模擬實現apache項目在瀏覽器的效果。
讀取node下的www目錄文件的代碼
let http=require('http')let fs=require('fs')//2.開始設置服務;let server=http.createServer()server.on('request',(req,resp)=>{ let url=req.url //若是是這個方式的話,去某個地方 /* * / index.html * /a.txt +/a.txt * /mi/login.html * /img/bg.jpg wwwdir+/img/bg.jpg * */ let wwwDir='D:/aaa/node/www' let filePath='/index.html' if(url!=='/'){ filePath=url } console.log(wwwDir+filePath) fs.readFile(wwwDir+filePath,(err,data)=>{ if(err){ console.log('404 Not Found!') } resp.end(data) })})//3.綁定機制;server.listen(3000,()=>{ console.log('啓動了...')})1234567891011121314151617181920212223242526272829303132
模板引擎不關心內容,只關心本身能認識的模板標記語法。Mustache是一個logic-less(輕邏輯)模板解析引擎,
它是爲了使用戶界面與業務數據(內容)分離而產生的,
它能夠生成特定格式的文檔,一般是標準的HTML文檔。
好比小程序的wxml中的代碼
{{userInfo.nickName}},這裏的{{ }}就是Mustache的語法。1
art-template 是一個簡約、超快的模板引擎。
它採用做用域預聲明的技術來優化模板渲染速度,從而得到接近 JavaScript 極限的運行性能,而且同時支持 NodeJS 和瀏覽器。
art-template 同時支持兩種模板語法。標準語法可讓模板更容易讀寫;原始語法具備強大的邏輯處理能力。
{{if user}}<h2>{{user.name}}</h2>{{/if}}123
<% if (user) { %><h2><%= user.name %></h2><% } %>123
使用步驟:
1.安裝
npm install art-template
2.在須要使用的文件中加載art-template
let temp=require(‘art-template’)
3.查文檔,使用art-template的API
在node-modules下的模板文件
<!DOCTYPE html><html style="height: 100%"><head> <meta charset="utf-8"> <!--<title>{{ title }}</title>--> <title><%=title%></title></head><body> <script src="../node_modules/art-template/lib/template-web.js"></script> <script type="text/template" id="tp1"> hello{{name}} 年齡:{{age}} 地址:{{address}} 愛好:{{each hobbies}}{{$value}}{{/each}} </script><!--測試--> <script> var ret=template('tp1',{name:'jack ma', age:22, address:'河南省鄭州市', hobbies:['吃好飯','唱好歌','開好車'] }) console.log(ret) </script></body></html>123456789101112131415161718192021222324252627
node.js使用art-template
let template=require('art-template')//template('script標籤id',{對象})let result=template.render('Hello:{{name}}',{name:'張晨光'})console.log(result)12345678
結合模板來使用
let template=require('art-template')let tempstr=`<!DOCTYPE html> <html style="height: 100%"> <head> <meta charset="utf-8"> <title><%=title%></title> </head> <body> hello{{name}} 年齡:{{age}} 地址:{{address}} 愛好:{{each hobbies}}{{$value}}{{/each}} </body> </html>`//template('script標籤id',{對象})let result=template.render(tempstr,{name:'jack ma', age:22, address:'河南省鄭州市', hobbies:['吃好飯','唱好歌','開好車']})console.log(result)123456789101112131415161718192021222324
不能使用這種方式,須要使用文件來讀寫
模板頁面:
<!DOCTYPE html><html style="height: 100%"><head> <meta charset="utf-8"> <title><%=title%></title></head><body> hello{{name}} 年齡:{{age}} 地址:{{address}} 愛好:{{each hobbies}}{{$value}}{{/each}} </body></html><!--測試--> <script> var ret=template('tp1',{name:'jack ma', age:22, address:'河南省鄭州市', hobbies:['吃好飯','唱好歌','開好車'] }) console.log(ret) </script>12345678910111213141516171819202122
node.js代碼;
let template=require('art-template')let fs=require('fs')fs.readFile('./template.html',(err,data)=>{ if(err){ return console.log('讀取文件有誤...') } //沒有問題,則這樣; //template('script標籤id',{對象}) let result=template.render(data.toString(),{name:'jack ma', age:22, address:'河南省鄭州市', hobbies:['吃好飯','唱好歌','開好車'] }) console.log(result)})