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