1
這是我的代碼:Lstat:類型錯誤:路徑必須是字符串。
var http = require('http');
var port = process.env.port || 1337;
var fs = require('fs');
var url = require('url');
var current_data_store = __dirname.replace(/\\/g,"/")+"/DATA";
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
this.requiredPath = current_data_store+""+url.format(req.url);
console.log(this.requiredPath);
fs.exists(this.requiredPath, function (exists) {
if(exists){
if(fs.lstatSync(this.requiredPath).isDirectory()){
console.log("It's dir");
//render list of files
} else if(fs.lstatSync(this.requiredPath).isFile()){
console.log("It's file");
//render file
}
} else {
console.log("doesnt exist");
}
});
res.end('Hello World\n'+req.url+'');
}).listen(port);
我想進行簡單的文件瀏覽器。腳本將顯示在地址欄中鍵入的路徑中的文件(但只有在DATA文件中的文件)。
的問題是:節點返回錯誤:
fs.js:679 return binding.lstat(pathModule._makeLong(path)); ^ TypeError: path must be a string at Object.fs.lstatSync (fs.js:679:18) at C:\Users\piotr_000\Source\Repos\Cloud\Cloud\server.js:15:19 at Object.cb [as oncomplete] (fs.js:168:19)
我真的不知道爲什麼出現。 fs.exists()函數可以正常工作,並且不會抱怨路徑不是字符串。
謝謝。一如既往:愚蠢的錯誤:) – Piotrek