首页 > nodejs中readdir的使用相对路径的问题

nodejs中readdir的使用相对路径的问题

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' } }

这信息怎么了?不就是你自己在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方法来获取路径


一般使用绝对路径
系统变量 __dirname 就是你启动项目的js文件的路径 你可以 拼接成各种路径
比如

var pathPublic = path.join(__dirname, '/../public/');
// 这样你就能用相对路径了 这样能取到当前路径上级的public目录路径
【热门文章】
【热门文章】