fs.readdir('../../angular-phonecat', function(err, fileList){
if (err) throw err;
console.log(fileList);
fileList.forEach(function(f) {
fs.stat(f, function(){
console.log(arguments);
})
})
})
报错信息
[ '.DS_Store',
'.bowerrc',
'.git',
'.gitignore',
'.jshintrc',
'.travis.yml',
'LICENSE',
'README.md',
'app',
'bower.json',
'karma-26468570',
'karma-41043336',
'node_modules',
'package.json',
'scripts',
'test' ]
{ '0': { [Error: ENOENT: no such file or directory, stat '.DS_Store'] errno: -2, code: 'ENOENT', syscall: 'stat', path: '.DS_Store' } }
{ '0': { [Error: ENOENT: no such file or directory, stat '.bowerrc'] errno: -2, code: 'ENOENT', syscall: 'stat', path: '.bowerrc' } }
{ '0': { [Error: ENOENT: no such file or directory, stat '.git'] errno: -2, code: 'ENOENT', syscall: 'stat', path: '.git' } }
{ '0': { [Error: ENOENT: no such file or directory, stat '.jshintrc'] errno: -2, code: 'ENOENT', syscall: 'stat', path: '.jshintrc' } }
{ '0':
{ [Error: ENOENT: no such file or directory, stat '.travis.yml']
errno: -2,
code: 'ENOENT',
syscall: 'stat',
path: '.travis.yml' } }
{ '0': { [Error: ENOENT: no such file or directory, stat '.gitignore'] errno: -2, code: 'ENOENT', syscall: 'stat', path: '.gitignore' } }
{ '0': { [Error: ENOENT: no such file or directory, stat 'LICENSE'] errno: -2, code: 'ENOENT', syscall: 'stat', path: 'LICENSE' } }
{ '0': { [Error: ENOENT: no such file or directory, stat 'README.md'] errno: -2, code: 'ENOENT', syscall: 'stat', path: 'README.md' } }
{ '0': { [Error: ENOENT: no such file or directory, stat 'app'] errno: -2, code: 'ENOENT', syscall: 'stat', path: 'app' } }
{ '0': { [Error: ENOENT: no such file or directory, stat 'bower.json'] errno: -2, code: 'ENOENT', syscall: 'stat', path: 'bower.json' } }
{ '0':
{ [Error: ENOENT: no such file or directory, stat 'karma-26468570']
errno: -2,
code: 'ENOENT',
syscall: 'stat',
path: 'karma-26468570' } }
{ '0':
{ [Error: ENOENT: no such file or directory, stat 'karma-41043336']
errno: -2,
code: 'ENOENT',
syscall: 'stat',
path: 'karma-41043336' } }
{ '0':
{ [Error: ENOENT: no such file or directory, stat 'node_modules']
errno: -2,
code: 'ENOENT',
syscall: 'stat',
path: 'node_modules' } }
{ '0':
{ [Error: ENOENT: no such file or directory, stat 'package.json']
errno: -2,
code: 'ENOENT',
syscall: 'stat',
path: 'package.json' } }
{ '0': { [Error: ENOENT: no such file or directory, stat 'scripts'] errno: -2, code: 'ENOENT', syscall: 'stat', path: 'scripts' } }
{ '0': { [Error: ENOENT: no such file or directory, stat 'test'] errno: -2, code: 'ENOENT', syscall: 'stat', path: 'test' } }
高洛峰2017-04-17 13:57:20
一般使用絕對路徑
系統變數 __dirname
就是你啟動專案的js檔案的路徑 你可以 拼接成各種路徑
例如
var pathPublic = path.join(__dirname, '/../public/');
// 这样你就能用相对路径了 这样能取到当前路径上级的public目录路径
PHPz2017-04-17 13:57:20
這訊息怎麼了?不就是你自己在console.log(arguments);
裡印出來的麼?如果你的疑問是,為什麼印出來的都是錯誤,而不是檔案真正的metadata
,那是你程式碼有問題,改成這樣:
fs.readdir('../../angular-phonecat', function(err, fileList){
if (err) throw err;
console.log(fileList);
fileList.forEach(function(f) {
fs.stat('../../angular-phonecat/' + f, function(){
console.log(arguments);
});
});
});
當然最好的方式不是這種字串“拼接”,最好使用
path
模組的join
或resolve
方法來取得路徑