node一些模块

child_process模块

  • 通过child_process模块创建子进程
  • childProcess.exec(shell命令,执行完毕后的回调函数)
const childProcess = require('child_process');

childProcess.exec('ls',function (error, stdout, stderr) {
    if (error !== null) {
        console.log('exec error: ' + error);
    }else {
       
    }
});

path模块

  • path模块的join方法用于基于当前系统的路径拼接
const  join = require('path').join;

join('a','b'); //'a/b'

fs模块

  • fs模块的readdirSync方法同步读取指定目录的子目录,返回子目录名称组成的数组。
    使用:
const fs = require("fs");
let files = fs.readdirSync(目录名);

递归读取文件夹上传文件

let files = fs.readdirSync(readPath);
 files.forEach( function(file){
   let info = fs.statSync(readPath+"/"+file)
   if(info.isDirectory()){
     uploadDir(readPath + "/" + file);
   }else{
     const fullFile =  join(readPath,file)
  // toDo 上传文件
     uploadFile(' fullFile)
   }
 })

shelljs模块

  • shelljs模块重新包装了 child_process,调用系统命令更加方便
  • shelljs模块的rm方法用于删除文件或者文件夹(fs.unlick只能删除文件不能删除文件夹)
const shell = require('shelljs');

 shell.rm("-rf",文件路径); //r表示递归删除所有文件,f表示强制删除

process.cwd()

  • process.cwd() 是当前执行node命令时候的文件夹绝对路径
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • https://nodejs.org/api/documentation.html 工具模块 Assert 测试 ...
    KeKeMars阅读 6,432评论 0 6
  • 1.运行一个nodejs文件, 如一个js文件中只含有console.log("hello world");的文件...
    不忘初心_9a16阅读 5,882评论 0 8
  • Node.js是目前非常火热的技术,但是它的诞生经历却很奇特。 众所周知,在Netscape设计出JavaScri...
    w_zhuan阅读 3,642评论 2 41
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,027评论 19 139
  • 昨天,经过漫长的等待,好几位在网上投诉的热心业主都不约而同地收到了12345平台统一的答复(跟三角镇住建局和居委会...
    东润业主不妥协阅读 500评论 0 4