nodejs之windows下使用fs.mkdir

來源:互聯網
上載者:User

在此之前需要說明一下,windows下使用nodejs取路徑時需要在前面加上一個'.'。這很奇怪,比如:'/cache/t' 直接這樣寫是執行不了的。需要變成:'./cache/t'。

windows下使用mkdir時發現根本建立不了一個目錄,但對比一下自己之前寫的例子,發現:當有多個路徑時,比如:'./cache/tt',如果目前的目錄下沒有cache目錄,則會在你寫的function裡執行throw err;

var fs = require('fs');fs.mkdir('./cache/tt', 0776, function(err){    if(err) throw err;// 如果沒有cache目錄,則拋出異常    else {    // my code    // 存在cache目錄時執行    }})

通過yss上面的分析,已經知道下面要做的事了。就是對每一層目錄迴圈建立了~

看下面代碼:

/* * * nkdir for windows (md command) * @params is the same as the mkdir*/function mkdirs(p, mode, callback){callback = callback || function(){};/* first should be . */if(p.charAt(0) != '.'){callback(p);return;}/* split it */var ps = p.split('/'), len = ps.length, i=1, tmp;while(i++<len){tmp = ps.slice(0,i).join('/');//console.log(tmp)//path.exists(tmp, function(exists){//if(!exists){fs.mkdir(tmp, mode);//console.log(i+": "+tmp)//}//});}callback();}

但是寫上面代碼的時候,我發現不能通過path.exists去做。如果把上面的'//'的注釋條去掉的話。輸出的結果:

E:\code\node>node e\http.js./cache./cache/ppt./cache/ppt/img./cache/ppt/img/imgloadlazy6: ./cache/ppt/img/imgloadlazy6: ./cache/ppt/img/imgloadlazy6: ./cache/ppt/img/imgloadlazy

很奇怪了吧~

這個問題有待yss之後去尋找,也希望看到這篇文章的你給我解答。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.