我正在阅读 Smashing Node.JS 书,并且在执行文件浏览器示例时不断收到参考错误。为什么会发生这种情况,我该如何纠正这个问题。我已经按照书中的示例进行操作,所以我对正在发生的事情有点迷茫
/**
* Module dependencies.
*/
var fs = require('fs')
, stdin = process.stdin
, stdout = process.stdout;
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) {
if (stat.isDirectory()) {
console.log(' '+i+' \033[36m' + filename + '/\033[39m');
} else {
console.log(' '+i+' \033[90m' + filename + '\033[39m')
}
if (++i == files.length) {
read();
} else{
file(i);
}
});
}
file(0);
});
function read() {
console.log('');
stdout.write(' \033[33mEnter your choice: \033[39m');
stdin.resume();
stdout.setEncoding('utf8');
stdin.on('data', option);
function option( data ) {
if (typeof files[Number(data)] !== "undefined" ) {
stdout.write(' \033[31mEnter your choice: \033[39m');
} else {
stdin.pause();
}
}
}