关于node.js

/**

  * Module dependencies 

**/

var fs = require("fs")

,stdin = process.stdin

,stdout = process.stdout;

var stats = [];

fs.readdir(process.cwd(),function  (err, files) {

console.log('');

if(!files.length){

return console.log('   \033[31m No files to show!\033[39m\n');

}


console.log('   Select which file or directory you want to see\n');

function file(i){

var filename = files[i];


fs.stat(__dirname + '/' + filename, function  (err, stat) {

stats[i] = stat;

if(stat.isDirectory()){

console.log('    '+i+'    \033[36m' + filename + '/\033[39m');

}else{

console.log('    '+i+'    \033[90m' + filename + '\033[39m');

}

i++;

if (i == files.length) {

read();

}else{

file(i);

}

});

}

file(0);

});

function read () {

console.log('');

stdout.write('    \033[33mEnter your choise: \033[39m');

stdin.resume();

stdin.setEncoding('utf8');

stdin.on('data', option);

}

function option (data) {

var filename = files[Number(data)];

if(!filename){

stdout.write('   \033[31mEnter your choise: \033[39m');

}else{

stdin.pause();

fs.readFile(__dirname + '/' + filename, 'utf8',function  (err, data) {

if(stats[Number(data)].isDirectory()){

fs.readdir(__dirname + '/' + filename, function (err, files) {

console.log('');

console.log('    ('+ files.length + ' files)');

files.forEach(function  (file) {

console.log('     -  '+ file);

});

console.log('');

})

}else{

fs.readFile(__dirname + '/' + filename, 'utf8', function  (err ,data) {

console.log('');

console.log('\033[90m' + data.replace(/(.*)/g, '     $1')+ '\033[39m');

})

}


})

为什么报错,下划线代码的files is not defined 

BangKk
浏览 1558回答 1
1回答

哿尘雨

你的files数组不是上一个函数里定义的吗?这个函数里当然未定义啦
打开App,查看更多内容
随时随地看视频慕课网APP