路径模块
// 引入path模块
const path = require('path');
// D:\Study\Path\app.js
// __filename 绝对路径
console.log(__filename); // D:\Study\Path\app.js
// path.basename() 返回path的最后一部分 app.js
console.log(path.basename(__filename));
// path.dirname() 获取目录名称 D:\Study\Path
console.log(path.dirname(__filename));
// path.extname() 获取文件的扩展名 .js
console.log(path.extname(__filename));
// // path.parse() 返回一个对象
console.log(path.parse(__filename));
// {
// root: 'D:\\',
// dir: 'D:\\Study\\Path',
// base: 'app.js',
// ext: '.js',
// name: 'app'
// }
// 获得具体值 app.js
console.log(path.parse(__filename).base);
// 5.path.join() 路径的拼接 D:\Study\Path\test\hello.html
console.log(path.join(__dirname, 'test', 'hello.html'));
文件模块
// 引入文件模块
const fs = require('fs');
// 引入路径模块
const path = require('path');
// fs.mkdir() 创建异步文件夹
fs.mkdir(path.join(__dirname, '/test'), {}, err => {
if (err) {
throw err
}
else {
console.log('文件创建成功');
}
})
// fs.writeFile() 写入异步创建文件
fs.writeFile(path.join(__dirname, '/test', 'hello.txt'), 'NodeJs', err => {
if (err) {
throw err;
}
else {
console.log('文件已经被写入');
}
// 继续写入文件内容 fs.appendFile(路径,内容,回调函数)
fs.appendFile(path.join(__dirname, '/test', 'hello.txt'), ' Hello', err => {
if (err) {
throw err;
}
else {
console.log('文件已经被写入');
}
})
})
// fs.readFile(路径,字符编码,回调函数)读取文件 不设置字符编码返回二进制
fs.readFile(path.join(__dirname, '/test', 'hello.txt'), 'UTF8', (err, data) => {
if (err) {
throw err;
}
else {
console.log(data);
}
})
// fs.rename(旧路径,新路径,回调函数) 文件重命名
fs.rename(path.join(__dirname, '/test', 'hello.txt'), path.join(__dirname, '/test', 'hi.txt'), err => {
if (err) {
throw err
}
else {
console.log('文件创建成功');
}
}
);
OS模块
// 引入模块
const os = require('os');
// 1.os.platform() 返回标识操作系统平台的字符串 win32
console.log(os.platform());
// //2. os.arch() 获得CPU的架构信息 x64
console.log(os.arch());
// // 3.os.cpus() 获得内核信息
console.log(os.cpus());
// [
// {
// model: 'Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz',
// speed: 2808,
// times: {
// user: 121568515,
// nice: 0,
// sys: 126643187,
// idle: 752083171,
// irq: 9463296
// }
// },
// {
// model: 'Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz',
// speed: 2808,
// times: {
// user: 124447453,
// nice: 0,
// sys: 106861640,
// idle: 768985390,
// irq: 3411156
// }
// },
// {
// model: 'Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz',
// speed: 2808,
// times: {
// user: 134553609,
// nice: 0,
// sys: 112501734,
// idle: 753239140,
// irq: 940375
// }
// },
// {
// model: 'Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz',
// speed: 2808,
// times: {
// user: 138234281,
// nice: 0,
// sys: 115043609,
// idle: 747016593,
// irq: 608640
// }
// }
// ]
// // 4.os.freemem() 获得内存信息 返回字节形式 返回系统空闲内存 2368356352
console.log(os.freemem());
// // 5.os.totalmem() 获得系统的总内存 8469585920
console.log(os.totalmem());
// //6. os.homedir() 获得主目录 字符串路径 C:\Users\hp
console.log(os.homedir());
// // 7.os.uptime() 返回系统正常运行时间 秒 3115831
console.log(os.uptime());
URL模块
// 导入模块
const url = require('url');
// 实例化url对象
const myUrl = new URL('http://myWebsit.com:8000/hello.html?id=100&status=active');
//1. myUrl.href() 序列化url ----- http://mywebsit.com:8000/hello.html?id=100&status=active
console.log(myUrl.href);
console.log(myUrl.toString()); //字符串方式显示
// 2.myUrl.host 主机部分包括端口号 ----- mywebsit.com:8000
console.log(myUrl.host);
// 3.myUrl.hostname 主机部分不包括端口号 ----- mywebsit.com
console.log(myUrl.hostname);
// 4.myUrl.pathname ----- /hello.html
console.log(myUrl.pathname);
// 5.myUrl.search ----- ?id=100&status=active
console.log(myUrl.search);
// 6.myUrl.searchParams ----- URLSearchParams { 'id' => '100', 'status' => 'active' }
console.log(myUrl.searchParams);
// 方法
// 7.myUrl.xxxx.append() 添加一个新参数 -----
myUrl.searchParams.append('abc', '123')
console.log(myUrl.searchParams);
myUrl.searchParams.forEach((value, name) => {
console.log(`${name}`, `${value}`);
})
Event模块
// 导入模块
const EventEmitter = require('events');
// 创建Emitter类
class MyEmitter extends EventEmitter { }
// 实例化对象
const myEmitter = new MyEmitter();
// 注册事件
myEmitter.on('event', (msg) => {
// 异步操作
setImmediate(() => {
console.log(msg);
})
})
// 实现事件的触发 ('与注册事件名称一致',)
myEmitter.emit('event', '实现事件并传递参数到注册事件到回到函数中');
console.log(1);
本文介绍了Node.js中的路径模块,包括引入、获取文件名、目录名、扩展名、解析文件路径等操作。同时,详细展示了文件模块的使用,如创建文件夹、写入和追加文件内容、读取文件以及重命名文件。此外,还讲解了OS模块,包括获取操作系统平台、CPU架构、内存信息和系统运行时间。最后,提到了URL模块,展示了如何解析和操作URL。
4345

被折叠的 条评论
为什么被折叠?



