编程教育资源分享平台

网站首页 > 后端开发 正文

NodeJS,模块,os操作系统模块、path简化路径、fs文件操作模块

luoriw 2024-02-01 14:31:15 后端开发 14 ℃ 0 评论

NodeJS

操作:windows10,node版本:v14.16.1,npm版本:6.14.12,yarn版本:1.22.17。

参考:http://nodejs.cn/api/process.html、https://github.com/alsotang/node-lessons

os操作系统模块

案例代码:

let os = require('os');

//// http://nodejs.cn/api/os.html

// 操作系统特定的行尾标记,POSIX 上是 \n,Windows 上是 \r\n
console.log("操作系统特定的行尾标记:",os.EOL);
// 包含用于错误码、进程信号等的常用操作系统特定常量。
console.log("操作系统特定常量:",JSON.stringify(os.constants));
// Windows上是 \\.\nul,POSIX上是 /dev/null
console.log("空设备的特定于平台的文件路径:",os.devNull);
// 操作系统平台的字符串
console.log("操作系统平台的字符串:",os.platform());
// 操作系统,Windows_NT
console.log("操作系统:",os.type());
// 操作系统,10.0.19044
console.log("操作系统:",os.release());
// 操作系统,Windows 10 Home China
console.log("操作系统:",os.version());

// 操作系统CPU架构,x64
console.log("操作系统CPU架构:",os.arch());
//每个逻辑 CPU 内核的信息的对象数组
console.log("每个逻辑CPU内核的信息:",JSON.stringify(os.cpus()));
// 可能的值是大端序的 'BE' 和小端序的 'LE'
console.log("CPU的字节序的字符串:",os.endianness());

// 以字节为单位
console.log("系统内存总量:",os.totalmem());
// 以字节为单位,3614236672
console.log("空闲的系统内存量:",os.freemem());

// 
console.log("系统正常运行时间:",os.uptime());

// 操作系统环境变量定义
console.log("用户的主目录:",os.homedir());
// 操作系统的主机名
console.log("操作系统的主机名:",os.hostname());
// 平均负载是 Unix特有的概念,Windows上,返回值始终为:[0, 0, 0]
console.log("操作系统的平均负载:",os.loadavg());

// 要为其检索调度优先级的进程ID
console.log("当前进程的优先级:",os.getPriority());

// 网络接口
console.log("网络地址的网络接口:",JSON.stringify(os.networkInterfaces()));

运行输出内容:

操作系统特定的行尾标记:

操作系统特定常量: {"UV_UDP_REUSEADDR":4,"dlopen":{},"errno":{"E2BIG":7,"EACCES":13,"EADDRINUSE":100,"EADDRNOTAVAIL":101,"EAFNOSUPPORT":102,"EAGAIN":11,"EALREADY":103,"EBADF":9,"EBADMSG":104,"EBUSY":16,"ECANCELED":105,"ECHILD":10,"ECONNAB
ORTED":106,"ECONNREFUSED":107,"ECONNRESET":108,"EDEADLK":36,"EDESTADDRREQ":109,"EDOM":33,"EEXIST":17,"EFAULT":14,"EFBIG":27,"EHOSTUNREACH":110,"EIDRM":111,"EILSEQ":42,"EINPROGRESS":112,"EINTR":4,"EINVAL":22,"EIO":5,"EISCONN":113,"EISDIR":21,"ELOOP":114,"EMFILE":24,"EMLINK":31,"EMSGSIZE":115,"ENAMETOOLONG":38,"ENETDOWN":116,"ENETRESET":117,"ENETUNREACH":118,"ENFILE":23,"ENOBUFS":119,"ENODATA":120,"ENODEV":19,"ENOENT":2,"ENOEXEC":8,"ENOLCK":39,"ENOLINK":121,"ENOMEM":12,"ENOMSG":122,"ENOPROTOOPT":123,"ENOSPC":28,"ENOSR":124,"ENOSTR":125,"ENOSYS":40,"ENOTCONN":126,"ENOTDIR":20,"ENOTEMPTY":41,"ENOTSOCK":128,"ENOTSUP":129,"ENOTTY":25,"ENXIO":6,"EOPNOTSUPP":130,"EOVERFLOW":132,"EPERM":1,"EPIPE":32,"EPROTO":134,"EPROTONOSUPPORT":135,"EPROTOTYPE":136,"ERANGE":34,"EROFS":30,"ESPIPE":29,"ESRCH":3,"ETIME":137,"ETIMEDOUT":138,"ETXTBSY":139,"EWOULDBLOCK":140,"EXDEV":18,"WSAEINTR":10004,"WSAEBADF":10009,"WSAEACCES":10013,"WSAEFAULT":10014,"WSAEINVAL":10022,"WSAEMFILE":10024,"WSAEWOULDBLOCK":10035,"WSAEINPROGRESS":10036,"WSAEALREADY":10037,"WSAENOTSOCK":10038,"WSAEDESTADDRREQ":10039,"WSAEMSGSIZE":10040,"WSAEPROTOTYPE":10041,"WSAENOPROTOOPT":10042,"WSAEPROTONOSUPPORT":10043,"WSAESOCKTNOSUPPORT":10044,"WSAEOPNOTSUPP":10045,"WSAEPFNOSUPPORT":10046,"WSAEAFNOSUPPORT":10047,"WSAEADDRINUSE":10048,"WSAEADDRNOTAVAIL":10049,"WSAENETDOWN":10050,"WSAENETUNREACH":10051,"WSAENETRESET":10052,"WSAECONNABORTED":10053,"WSAECONNRESET":10054,"WSAENOBUFS":10055,"WSAEISCONN":10056,"WSAENOTCONN":10057,"WSAESHUTDOWN":10058,"WSAETOOMANYREFS":10059,"WSAETIMEDOUT":10060,"WSAECONNREFUSED":10061,"WSAELOOP":10062,"WSAENAMETOOLONG":10063,"WSAEHOSTDOWN":10064,"WSAEHOSTUNREACH":10065,"WSAENOTEMPTY":10066,"WSAEPROCLIM":10067,"WSAEUSERS":10068,"WSAEDQUOT":10069,"WSAESTALE":10070,"WSAEREMOTE":10071,"WSASYSNOTREADY":10091,"WSAVERNOTSUPPORTED":10092,"WSANOTINITIALISED":10093,"WSAEDISCON":10101,"WSAENOMORE":10102,"WSAECANCELLED":10103,"WSAEINVALIDPROCTABLE":10104,"WSAEINVALIDPROVIDER":10105,"WSAEPROVIDERFAILEDINIT":10106,"WSASYSCALLFAILURE":10107,"WSASERVICE_NOT_FOUND":10108,"WSATYPE_NOT_FOUND":10109,"WSA_E_NO_MORE":10110,"WSA_E_CANCELLED":10111,"WSAEREFUSED":10112},"signals":{"SIGHUP":1,"SIGINT":2,"SIGILL":4,"SIGABRT":22,"SIGFPE":8,"SIGKILL":9,"SIGSEGV":11,"SIGTERM":15,"SIGBREAK":21,"SIGWINCH":28},"priority":{"PRIORITY_LOW":19,"PRIORITY_BELOW_NORMAL":10,"PRIORITY_NORMAL":0,"PRIORITY_ABOVE_NORMAL":-7,"PRIORITY_HIGH":-14,"PRIORITY_HIGHEST":-20}}
空设备的特定于平台的文件路径: undefined
操作系统平台的字符串: win32
操作系统: Windows_NT
操作系统: 10.0.19044
操作系统: Windows 10 Home China
操作系统CPU架构: x64
每个逻辑CPU内核的信息: [{"model":"11th Gen Intel(R) Core(TM) i5-11400 @ 2.60GHz","speed":2592,"times":{"user":5920609,"nice":0,"sys":5940109,"idle":342238562,"irq":699609}},{"model":"11th Gen Intel(R) Core(TM) i5-11400 @ 2.60GHz","speed"
:2592,"times":{"user":3800250,"nice":0,"sys":3504046,"idle":346794796,"irq":75562}},{"model":"11th Gen Intel(R) Core(TM) i5-11400 @ 2.60GHz","speed":2592,"times":{"user":11586609,"nice":0,"sys":7944781,"idle":334567703,"irq":197921}},{"model":"11th Gen Intel(R) Core(TM) i5-11400 @ 2.60GHz","speed":2592,"times":{"user":4997218,"nice":0,"sys":3172218,"idle":345929656,"irq":44062}},{"model":"11th Gen Intel(R) Core(TM) i5-11400 @ 2.60GHz","speed":2592,"times":{"user":8229328,"nice":0,"sys":7743578,"idle":338126171,"irq":132437}},{"model":"11th Gen Intel(R) Core(TM) i5-11400 @ 2.60GHz","speed":2592,"times":{"user":4806796,"nice":0,"sys":5465437,"idle":343826859,"irq":45531}},{"model":"11th Gen Intel(R) Core(TM) i5-11400 @ 2.60GHz","speed":2592,"times":{"user":7826390,"nice":0,"sys":7145359,"idle":339127343,"irq":101390}},{"model":"11th Gen Intel(R) Core(TM) i5-11400 @ 2.60GHz","speed":2592,"times":{"user":5070687,"nice":0,"sys":6021812,"idle":343006593,"irq":60093}},{"model":"11th Gen Intel(R) Core(TM) i5-11400 @ 2.60GHz","speed":2592,"times":{"user":6026218,"nice":0,"sys":5566046,"idle":342506812,"irq":99812}},{"model":"11th Gen Intel(R) Core(TM) i5-11400 @ 2.60GHz","speed":2592,"times":{"user":4702890,"nice":0,"sys":3656031,"idle":345740156,"irq":51953}},{"model":"11th Gen Intel(R) Core(TM) i5-11400 @ 2.60GHz","speed":2592,"times":{"user":6993187,"nice":0,"sys":6319359,"idle":340786531,"irq":118812}},{"model":"11th Gen Intel(R) Core(TM) i5-11400 @ 2.60GHz","speed":2592,"times":{"user":4840234,"nice":0,"sys":5985484,"idle":343273359,"irq":94671}}]
CPU的字节序的字符串: LE
系统内存总量: 16887386112
空闲的系统内存量: 3208261632
系统正常运行时间: 494495
用户的主目录: C:\Users\dd
操作系统的主机名: DESKTOP-SUEMMUL
操作系统的平均负载: [ 0, 0, 0 ]
当前进程的优先级: 0
网络地址的网络接口: {"以太网":[{"address":"fe80::8969:a713:7eef:c74f","netmask":"ffff:ffff:ffff:ffff::","family":"IPv6","mac":"b0:7b:25:16:7d:55","internal":false,"cidr":"fe80::8969:a713:7eef:c74f/64","scopeid":11},{"address":"192.168.3.
50","netmask":"255.255.255.0","family":"IPv4","mac":"b0:7b:25:16:7d:55","internal":false,"cidr":"192.168.3.50/24"}],"Loopback Pseudo-Interface 1":[{"address":"::1","netmask":"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff","family":"IPv6","mac":"00:00:00:00:00:00","internal":true,"cidr":"::1/128","scopeid":0},{"address":"127.0.0.1","netmask":"255.0.0.0","family":"IPv4","mac":"00:00:00:00:00:00","internal":true,"cidr":"127.0.0.1/8"}]}

path简化路径模块

案例代码:

const path = require('path');

//// http://nodejs.cn/api/path.html

// windows tmp1.txt
console.log(path.basename('D:/MyData/tmp1.txt'));
// windows tmp1.txt
console.log(path.win32.basename('D:/MyData/tmp1.txt'));
// windows tmp1.txt
console.log(path.posix.basename('D:/MyData/tmp1.txt'));

// 环境变量 
console.log(process.env.PATH);
console.log(process.env.PATH.split(path.delimiter));

// D:/MyData
console.log("目录名:",path.dirname('D:/MyData/tmp1.txt'));
// .txt
console.log("扩展名:",path.extname('D:/MyData/tmp1.txt'));
// false
console.log("绝对路径:",path.isAbsolute('.'));
// 1\2\3\4\5
console.log("路径片段:",path.join('1', '2', '3', '4', '5'));
// D:\MyData\tmp1.txt
console.log("合法路径:",path.normalize('D:///MyData//tmp1.txt'));
// 路径解析
console.log("路径解析:",JSON.stringify(path.parse('D:/MyData/tmp1.txt')));

运行输出内容:

tmp1.txt
tmp1.txt
tmp1.txt
D:\Apps\bin\cmder_mini\vendor\conemu-maximus5\ConEmu\Scripts;D:\Apps\bin\cmder_mini\vendor\conemu-maximus5;D:\Apps\bin\cmder_mini\vendor\conemu-maximus5\ConEmu;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;D:\Apps\Java\Java\jdk1.8.0_131\bin;D:\ProgramFiles\Git\cmd;D:\Apps\mysql-5.7.13-winx64\bin;D:\Apps\Java\Maven\apache-maven-3.5.4\bin;D:\Apps\Node\node-v14.16.1-win-x64\;D:\ProgramFiles\Git\bin;D:\Apps\bin;D:\Apps\erl-24.0\bin;D:\ProgramFiles\Git\mingw64\bin;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;D:\Apps\bin\cmder_mini\;D:\Apps\Python\Python39;D:\Apps\MongoDB\mongodb-win32-x86_64-enterprise-windows-4.4.13\bin;C:\Users\dd\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Java\jdk1.8.0_131\lib;C:\Users\dd\.dotnet\tools;D:\ProgramFiles\Git\usr\bin;D:\Apps\bin\cmder_mini\vendor\bin;D:\Apps\bin\cmder_mini
[
  'D:\\Apps\\bin\\cmder_mini\\vendor\\conemu-maximus5\\ConEmu\\Scripts',
  'D:\\Apps\\bin\\cmder_mini\\vendor\\conemu-maximus5',
  'D:\\Apps\\bin\\cmder_mini\\vendor\\conemu-maximus5\\ConEmu',
  'C:\\ProgramData\\Oracle\\Java\\javapath',
  'C:\\Windows\\system32',
  'C:\\Windows',
  'C:\\Windows\\System32\\Wbem',
  'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\',
  'C:\\Windows\\System32\\OpenSSH\\',
  'D:\\Apps\\Java\\Java\\jdk1.8.0_131\\bin',
  'D:\\ProgramFiles\\Git\\cmd',
  'D:\\Apps\\mysql-5.7.13-winx64\\bin',
  'D:\\Apps\\Java\\Maven\\apache-maven-3.5.4\\bin',
  'D:\\Apps\\Node\\node-v14.16.1-win-x64\\',
  'D:\\ProgramFiles\\Git\\bin',
  'D:\\Apps\\bin',
  'D:\\Apps\\erl-24.0\\bin',
  'D:\\ProgramFiles\\Git\\mingw64\\bin',
  'C:\\Program Files\\dotnet\\',
  'C:\\Program Files\\Microsoft SQL Server\\130\\Tools\\Binn\\',
  'C:\\Program Files\\Microsoft SQL Server\\Client SDK\\ODBC\\170\\Tools\\Binn\\',
  'D:\\Apps\\bin\\cmder_mini\\',
  'D:\\Apps\\Python\\Python39',
  'D:\\Apps\\MongoDB\\mongodb-win32-x86_64-enterprise-windows-4.4.13\\bin',
  'C:\\Users\\dd\\AppData\\Local\\Microsoft\\WindowsApps',
  'C:\\Program Files\\Java\\jdk1.8.0_131\\lib',
  'C:\\Users\\dd\\.dotnet\\tools',
  'D:\\ProgramFiles\\Git\\usr\\bin',
  'D:\\Apps\\bin\\cmder_mini\\vendor\\bin',
  'D:\\Apps\\bin\\cmder_mini'
]
目录名: D:/MyData
扩展名: .txt
绝对路径: false
路径片段: 1\2\3\4\5
合法路径: D:\MyData\tmp1.txt
路径解析: {"root":"D:/","dir":"D:/MyData","base":"tmp1.txt","ext":".txt","name":"tmp1"}

fs文件操作模块

代码案例,读取文件:

let fs = require("fs");

// 读取文件
fs.readFile("D:/MyData/tmp1.txt",function(err,data){
	if(err){
		throw err;
	}
	console.log("data=",data);  // <Buffer
	console.log("string=",data.toString()); // 输出string
});

// 读取文件==>设置编码
fs.readFile("D:/MyData/tmp1.txt","UTF-8",function(err,data){
	if(err){
		throw err;
	}
	console.log("UTF-8 data=",data);  // 输出string
	console.log("UTF-8 string=",data.toString()); // 输出string
});

代码案例,写入文件:

let fs = require("fs");

let string = "乌克兰和俄罗斯的根本矛盾~\r\n乌克兰和俄罗斯同根同源,都属于斯拉夫人中的东斯拉夫人。\r\n";
string = string + "从古至今两国在合并分裂、再合并再分裂中度过漫长的千年。\r\n";
// 文件不存在会创建一个文件
fs.writeFile("D:/MyData/tmp1.txt",string, function(err){
	if(err){
		throw err;
	}
	console.log('文件已保存~');
});

// 默认flag='w'是写,会清空文件,然后再写入
// 传递了追加参数 { 'flag': 'a' }====>已追加的方式输出到文件
fs.writeFile("D:/MyData/tmp2.txt", string, { 'flag': 'a' }, function(err){
	if(err){
		throw err;
	}
	console.log('文件已保存~');
});

代码案例,创建目录、读取目录:

var fs = require('fs'); 

// 创建目录
fs.mkdir('D:/MyData/newTmp1', function(err) {
    if (err) {
        throw err;
    }
    console.log('创建目录成功~');
});

// 读取目录
fs.readdir('D:/MyData/', function(err, files) {
    if (err) {
        throw err;
    }
    // files是数组,每个元素是此目录下的文件或文件夹的名称
    console.log("files",files);
});

代码案例,删除文件:

let fs = require('fs');

// 删除文件====>异步操作
fs.unlink("D:/MyData/tmp1.txt",function(err){
	if(err){
		throw err;
	}
	console.log("异步操作,删除成功了文件!");
});

// 删除文件====>同步操作
fs.unlinkSync("D:/MyData/tmp2.txt");
console.log("同步操作,删除成功了文件!");

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表
最新留言