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
Usually use absolute paths
System variables __dirname
is the path to the js file where you start the project. You can splice it into various paths
such as
var pathPublic = path.join(__dirname, '/../public/');
// 这样你就能用相对路径了 这样能取到当前路径上级的public目录路径
PHPz2017-04-17 13:57:20
What happened to this message? Wasn't it just you who printed it out in console.log(arguments);
? If your question is why all errors are printed instead of the real metadata
of the file, then there is something wrong with your code. Change it to this:
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);
});
});
});
Of course the best way is not this kind of string "splicing". It is best to use the
path
orjoin
method of theresolve
module to get the path