• Node.js 中的重要API:命令行工具以及FS API 首个Node应用


    2019-12-16

    17:05:54

     

     

     

     

     

     

     

     

    var fs = require('fs');
    fs.readdir(__dirname,function(err,files){
        console.log(files);
    });

     

     

     

     

    /**
     * Module dependencies.
     */
    
    var fs = require('fs');
    
    fs.readdir(__dirname, function (err, files) {
      console.log('');
    
      if (!files.length) {
        return console.log('    33[31m No files to show!33[39m
    ');
      }
    
      console.log('   Select which file or directory you want to see
    ');
    
      function file(i) {
        var filename = files[i];
    
        fs.stat(__dirname + '/' + filename, function (err, stat) {
          if (stat.isDirectory()) {
            console.log('     '+i+'   33[36m' + filename + '/33[39m');
          } else {
            console.log('     '+i+'   33[90m' + filename + '33[39m');
          }
    
          if (++i == files.length) {
            console.log('');
            process.stdout.write('   33[33mEnter your choice: 33[39m');
            process.stdin.resume();
            process.stdin.setEncoding('utf8');
          } else {
            file(i);
          }
        });
      }
    
      file(0);
    
    });

     

     

    /**
     * Module dependencies.
     */
    
    var fs = require('fs')
      , stdin = process.stdin
      , stdout = process.stdout
    
    /**
     * Read the current directory.
     */
    
    fs.readdir(__dirname, function (err, files) {
      console.log('');
    
      if (!files.length) {
        return console.log('    33[31m No files to show!33[39m
    ');
      }
    
      console.log('   Select which file or directory you want to see
    ');
    
      // called for each file walked in the directory
      function file(i) {
        var filename = files[i];
    
        fs.stat(__dirname + '/' + filename, function (err, stat) {
          if (stat.isDirectory()) {
            console.log('     '+i+'   33[36m' + filename + '/33[39m');
          } else {
            console.log('     '+i+'   33[90m' + filename + '33[39m');
          }
    
          if (++i == files.length) {
            read();
          } else {
            file(i);
          }
        });
      }
    
      // read user input when files are shown
      function read () {
        console.log('');
        stdout.write('   33[33mEnter your choice: 33[39m');
    
        stdin.resume();
        stdin.setEncoding('utf8');
        stdin.on('data', option);
      }
    
      // called with the option supplied by the user
      function option (data) {
        if (!files[Number(data)]) {
          stdout.write('   33[31mEnter your choice: 33[39m');
        } else {
          stdin.removeListener('data', option);
        }
      }
    
      // start by walking the first file
      file(0);
    });

     

     完成:

    /**
     * Module dependencies.
     */
    
    var fs = require('fs')
      , stdin = process.stdin
      , stdout = process.stdout
    
    /**
     * Read the current directory.
     */
    
    fs.readdir(__dirname, function (err, files) {
      console.log('');
    
      if (!files.length) {
        return console.log('    33[31m No files to show!33[39m
    ');
      }
    
      console.log('   Select which file or directory you want to see
    ');
    
      // called for each file walked in the directory
      function file(i) {
        var filename = files[i];
    
        fs.stat(__dirname + '/' + filename, function (err, stat) {
          if (stat.isDirectory()) {
            console.log('     '+i+'   33[36m' + filename + '/33[39m');
          } else {
            console.log('     '+i+'   33[90m' + filename + '33[39m');
          }
    
          if (++i == files.length) {
            read();
          } else {
            file(i);
          }
        });
      }
    
      // read user input when files are shown
      function read () {
        console.log('');
        stdout.write('   33[33mEnter your choice: 33[39m');
    
        stdin.resume();
        stdin.setEncoding('utf8');
        stdin.on('data', option);
      }
    
      // called with the option supplied by the user
      function option (data) {
        if (!files[Number(data)]) {
          stdout.write('   33[31mEnter your choice: 33[39m');
        } else {
          stdin.removeListener('data', option);
        }
      }
    
      // start by walking the first file
      file(0);
    });

     

      

     

     

     

      

     

     

  • 相关阅读:
    迅雷的工作原理 [揭密迅雷]
    揭密迅雷BT式下载本质 [揭密迅雷]
    2010年8月09日_周一_Toc control
    2010年8月08日_周日_MapCopyrightText control
    2010年8月08日_周日_Magnifier control
    2010年8月07日_周六_HoverExpandExtender control
    2010年8月11日_周三_ZoomLevel_control
    ArcGIS Server网站发布后地图显示空白的原因之一
    2010年8月10日_周二_TaskManagercontrol
    搭建一个简单的callBack函数
  • 原文地址:https://www.cnblogs.com/JasonPeng1/p/12051041.html
Copyright © 2020-2023  润新知