From d8c2e3e47c946445b64aa28171f70a6656fdb83a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E5=93=B2?= Date: Sun, 22 Mar 2026 23:00:52 +0800 Subject: [PATCH 1/2] 1 --- .../202603116-hug\357\274\237.md" | 0 ...202603118-HTTP\346\250\241\345\235\227.md" | 0 ...44\350\241\214\345\267\245\345\205\267.md" | 0 ...05\347\275\256\346\250\241\345\235\227.md" | 261 ++++++++++++++++++ ...73\347\273\237\346\250\241\345\235\227.md" | 121 ++++++++ 5 files changed, 382 insertions(+) create mode 100644 "\351\231\210\346\200\235\345\223\262/202603116-hug\357\274\237.md" create mode 100644 "\351\231\210\346\200\235\345\223\262/202603118-HTTP\346\250\241\345\235\227.md" rename "\351\231\210\346\200\235\345\223\262/202603013-\345\221\275\344\273\244\350\241\214\345\267\245\345\205\267.md" => "\351\231\210\346\200\235\345\223\262/20260313-\345\221\275\344\273\244\350\241\214\345\267\245\345\205\267.md" (100%) create mode 100644 "\351\231\210\346\200\235\345\223\262/20260319-\345\237\272\346\234\254\345\206\205\347\275\256\346\250\241\345\235\227.md" create mode 100644 "\351\231\210\346\200\235\345\223\262/20260320-\346\226\207\344\273\266\347\263\273\347\273\237\346\250\241\345\235\227.md" diff --git "a/\351\231\210\346\200\235\345\223\262/202603116-hug\357\274\237.md" "b/\351\231\210\346\200\235\345\223\262/202603116-hug\357\274\237.md" new file mode 100644 index 0000000..e69de29 diff --git "a/\351\231\210\346\200\235\345\223\262/202603118-HTTP\346\250\241\345\235\227.md" "b/\351\231\210\346\200\235\345\223\262/202603118-HTTP\346\250\241\345\235\227.md" new file mode 100644 index 0000000..e69de29 diff --git "a/\351\231\210\346\200\235\345\223\262/202603013-\345\221\275\344\273\244\350\241\214\345\267\245\345\205\267.md" "b/\351\231\210\346\200\235\345\223\262/20260313-\345\221\275\344\273\244\350\241\214\345\267\245\345\205\267.md" similarity index 100% rename from "\351\231\210\346\200\235\345\223\262/202603013-\345\221\275\344\273\244\350\241\214\345\267\245\345\205\267.md" rename to "\351\231\210\346\200\235\345\223\262/20260313-\345\221\275\344\273\244\350\241\214\345\267\245\345\205\267.md" diff --git "a/\351\231\210\346\200\235\345\223\262/20260319-\345\237\272\346\234\254\345\206\205\347\275\256\346\250\241\345\235\227.md" "b/\351\231\210\346\200\235\345\223\262/20260319-\345\237\272\346\234\254\345\206\205\347\275\256\346\250\241\345\235\227.md" new file mode 100644 index 0000000..f82af8b --- /dev/null +++ "b/\351\231\210\346\200\235\345\223\262/20260319-\345\237\272\346\234\254\345\206\205\347\275\256\346\250\241\345\235\227.md" @@ -0,0 +1,261 @@ +# 基本内置模块 + +## console模块 + +- 基本输出方法 + +``` +console.log('普通'); + .info('提示'); + .Buffer('警告消息'); + .error('错误消息'); +``` + +- 格式化输出 + +``` +// %s 字符串 +console.log('%s 今天学习了 %d 小时', '小明', 2); + +// %d 数字 +console.log('进度: %d%%', 75); + +// %j JSON +console.log('%j', {name: '张三', age: 20}); +``` + +- 高级用法 + +``` +// 表格输出 +console.table([ + { name: '张三', age: 20, gender: '男' }, + { name: '李四', age: 25, gender: '女' }, + { name: '王五', age: 30, gender: '男' } +]); + +// 计数功能 +console.count('click'); +console.count('click'); +console.count('click'); +console.countReset('click'); +console.count('click'); + +// 计时功能 +console.time('loop'); +for (let i = 0; i < 10000; i++) {} +console.timeEnd('loop'); + +// 断言 +console.assert(1 === 1, '这不会显示'); +console.assert(1 === 2, '这会显示'); + +// 调用栈追踪 +function fn1() { fn2(); } +function fn2() { console.trace('调用追踪'); } +fn1(); +``` + +## process模块 + +- 进程信息 + +``` +process.argv()//命令行参数 +``` + +``` +// Node.js版本信息 +console.log(process.version); // v24.x.x +console.log(process.versions.node); // 24.x.x +console.log(process.versions.v8); // x.x.x + +// 平台信息 +console.log(process.platform); // win32 / darwin / linux +console.log(process.arch); // x64 / arm64 + +// 当前工作目录 +console.log(process.cwd()); // 返回当前工作目录 + +// 环境变量 +console.log(process.env.USER); // 用户名 +console.log(process.env.NODE_ENV); // 环境变量 + +``` + +- 进程控制 + +``` +// 退出程序 +process.exit(0); // 正常退出 +process.exit(1); // 异常退出 + +// 监听退出信号 +process.on('exit', (code) => { + console.log('程序即将退出,退出码:' + code); +}); + +process.on('SIGINT', () => { + console.log('收到Ctrl+C信号'); + process.exit(0); +}); + +// 监听未捕获的异常 +process.on('uncaughtException', (err) => { + console.error('未捕获的异常:', err); + process.exit(1); +}); +``` + +## Buffer模块 + +- 创建Buffer + +``` +// 从字符串创建 +const buf1 = Buffer.from('你好'); +console.log(buf1); // + +// 从字符串创建(指定编码) +const buf2 = Buffer.from('你好', 'utf8'); +console.log(buf2.toString()); // 你好 + +// 创建指定长度的Buffer +const buf3 = Buffer.alloc(10); // 10字节,初始值为0 +console.log(buf3); // + +// 不初始化(可能有旧数据,速度快) +const buf4 = Buffer.allocUnsafe(10); + +// 从数组创建 +const buf5 = Buffer.from([72, 101, 108, 108, 111]); // "Hello" +console.log(buf5.toString()); // Hello +``` + +- Buffer操作 + +``` +// 字符串转换 +const buf = Buffer.from('Hello'); +console.log(buf.toString()); // Hello +console.log(buf.toString('utf8', 0, 3)); // Hel + +// 获取长度 +console.log(Buffer.from('你好').length); // 6(中文占3字节) + +// 写入数据 +const buf = Buffer.alloc(10); +buf.write('Hello', 0, 5, 'utf8'); +console.log(buf.toString()); // Hello + +// 读取数据 +console.log(buf.readUInt8(0)); // 72 (H的ASCII) + +// 拼接Buffer +const buf1 = Buffer.from('Hello'); +const buf2 = Buffer.from(' World'); +const combined = Buffer.concat([buf1, buf2]); +console.log(combined.toString()); // Hello World +``` + +## path模块 + +- 路径基本操作 + +``` +const path = require('path'); + +// 获取目录 +console.log(__dirname); // 当前文件所在目录 +console.log(process.cwd()); // 当前工作目录 + +// 获取文件名 +console.log(__filename); // 当前文件完整路径 + +// path.join() - 拼接路径 +const fullPath = path.join(__dirname, 'src', 'index.js'); +console.log(fullPath); // .../src/index.js + +// path.resolve() - 解析为绝对路径 +const absPath = path.resolve('index.js'); +console.log(absPath); // C:/Users/.../index.js +``` + +- 路径解析 + +``` +const path = require('path'); +const filePath = '/Users/zhangsan/project/src/index.js'; + +// 获取文件名 +path.basename(filePath); // index.js +path.basename(filePath, '.js'); // index + +// 获取扩展名 +path.extname(filePath); // .js + +// 获取目录 +path.dirname(filePath); // /Users/zhangsan/project/src + +// 解析路径信息 +path.parse(filePath); +// { +// root: '/', +// dir: '/Users/zhangsan/project/src', +// base: 'index.js', +// ext: '.js', +// name: 'index' +// } +``` + +- 路径判断 + +``` +const path = require('path'); +const filePath = '/Users/zhangsan/project/index.js'; + +// 判断是否为绝对路径 +path.isAbsolute('/Users/zhangsan'); // true +path.isAbsolute('./index.js'); // false + +// 路径比较 +path.equals('/Users/zhangsan/a', '/Users/zhangsan/a'); // true +``` + +## 练习 + +--- + +- 选择题:BACBA + +- 简答题 + +1.请解释console.log和 + console.error的区别。 + +- log 输出普通日志 / 调试信息;error 输出错误信息(语义化区分)。 + +2.process.argv返回的数组包含哪些内容?请举例说明。 + +- 0:Node 可执行文件路径(如/usr/local/bin/node); + +- 1:当前执行的 JS 文件路径(如/project/app.js); + +- 2及以后:用户自定义参数(如执行node app.js name=张三,第 2 项为name=张三)。 + +3.Buffer和普通数组有什么区别? + +- 存储内容:数组存任意 JS 类型(数字 / 字符串 / 对象);Buffer 仅存二进制数据(字节),专门处理文件 / 网络流。 +长度固定:Buffer 创建后长度不可变;数组可动态增删(push/pop)。 + +4.__dirname和process.cwd()有什么区别? + +- __dirname:当前 JS 文件所在目录的绝对路径(文件级,固定不变)。 +process.cwd():Node 进程的当前工作目录(进程级,可通过cd命令改变)。 + +5.path.join()和path.resolve()有什么区别? + +- path.join():拼接路径片段,仅做字符串拼接,保留相对路径(不会自动解析为绝对路径)。 +示例:path.join('a', '../b') → 'b'。 +path.resolve():将路径片段解析为绝对路径(从当前工作目录开始,类似cd命令)。 +示例:path.resolve('a', '../b') → /当前目录/b(如/project/b);path.resolve('/a', 'b') → /a/b。 diff --git "a/\351\231\210\346\200\235\345\223\262/20260320-\346\226\207\344\273\266\347\263\273\347\273\237\346\250\241\345\235\227.md" "b/\351\231\210\346\200\235\345\223\262/20260320-\346\226\207\344\273\266\347\263\273\347\273\237\346\250\241\345\235\227.md" new file mode 100644 index 0000000..b85b730 --- /dev/null +++ "b/\351\231\210\346\200\235\345\223\262/20260320-\346\226\207\344\273\266\347\263\273\347\273\237\346\250\241\345\235\227.md" @@ -0,0 +1,121 @@ +# 文件系统模块 + +引入fs文件模块 + +``` +const fs = require('fs'); +//文件是否存在 +fs.existsSync() +``` + +## 同步读取 + +``` +const content = fs.readFileSync('./data.txt', 'utf8'); +console.log(content); +``` + +## 同步写入 + +``` +fs.writeFileSync('./data.txt', 'Hello World'); +``` + +## 文件操作 + +``` +const stats=fs.statSync('./data.txt'); +console.log('是否为文件',stats.isFile()); + ('是否目录',stats.isDirectory()); + ('文件大小:', stats.size, '字节'); + ('创建时间:', stats.birthtime); + ('修改时间:', stats.mtime); + +//删除文件 +fs.unlink('./temp.txt'); + +//读取文件 +let a = fs.readFileSync('./a.txt'); + +//写入文件 +fs.whiteFileSync(a,'./b.txt') + +//重命名 +fs.renameSync('./a.txt','./b.txt'); + +// 移动(跨目录重命名) +fs.renameSync('./a.txt', './backup/b.txt'); + +//创建目录 + +fs.mkdirSync('./abc'); + +//创建多级目录(recursive: true要多加一个这个) + +fs.mkdirSync ('./a/b/c', { recursive: true}); + +// 读取目录 + +fs.readdirSync('./src'); + +// 读取详细信息||withFileTypes: true(用数组显示目录下的文件其他文件) +const items = fs.readdirSync('./src', { withFileTypes: true }); + +//删除目录 +fs.rmdirSync('./a'); + + +``` + +## Watch监听文件变化 + +``` +// 监听文件变化 +fs.watch('./data.txt', (eventType, filename) => { + console.log('事件类型:', eventType); + console.log('文件名:', filename); +}); + +// 使用fs.watchFile(更详细) +fs.watchFile('./data.txt', { persistent: true }, (curr, prev) => { + console.log('文件修改时间变化了'); + console.log('之前:', prev.mtime); + console.log('现在:', curr.mtime); +}); + +// 停止监听 +fs.unwatchFile('./data.txt'); +``` + +## 练习 + +- 选择题:BBBBC + +- 简答题 + +1. 同步方法和异步方法的区别 + +- 阻塞后续代码执行,直到操作完成 +非阻塞,操作在后台执行,不等待 + +2. fs.readFile 和 fs.readFileSync 的区别 + +- fs.readFileSync(同步): +读取文件时阻塞后续代码,直接返回文件内容(Buffer / 字符串),出错则抛出异常,需用 try/catch 捕获。 +fs.readFile(异步): +读取文件不阻塞,通过回调函数接收结果((err, data) => {}),或 Promise 化(fs.promises.readFile),出错时 err 参数不为 null。 + +3. 如何实现文件的追加写入 + +- 同步:fs.appendFileSync(路径, 内容),文件不存在则创建,内容追加到末尾。 +替代方案:fs.writeFile(路径, 内容, { flag: 'a' })(flag: 'a' 表示追加模式)。 + +4. 什么是流式处理?它适合什么场景? + +- 流式处理:将文件 / 数据拆分成小块(chunk),分批次读取 / 写入,而非一次性加载到内存,核心是fs.createReadStream/fs.createWriteStream。 +适用场景:处理大文件(如 GB 级日志、视频)、网络传输(文件上传 / 下载)、实时数据处理(如日志流解析),避免内存溢出。 + +5. 如何监听文件的变化? + +- 核心方法:fs.watch(路径, (事件类型, 文件名) => {}),监听文件 / 目录的修改、删除等变化。 +补充:fs.watchFile(路径, (curr, prev) => {})(轮询方式,兼容性更好但性能稍差),可对比文件状态变化(如修改时间)。 -- Gitee From 23fe9c2ca94729602adee894674418a3889839a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E5=93=B2?= Date: Sun, 22 Mar 2026 23:01:58 +0800 Subject: [PATCH 2/2] 1 --- .../20260316-hug\357\274\237.md" | 0 .../20260318-HTTP\346\250\241\345\235\227.md" | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename "\351\231\210\346\200\235\345\223\262/202603116-hug\357\274\237.md" => "\351\231\210\346\200\235\345\223\262/20260316-hug\357\274\237.md" (100%) rename "\351\231\210\346\200\235\345\223\262/202603118-HTTP\346\250\241\345\235\227.md" => "\351\231\210\346\200\235\345\223\262/20260318-HTTP\346\250\241\345\235\227.md" (100%) diff --git "a/\351\231\210\346\200\235\345\223\262/202603116-hug\357\274\237.md" "b/\351\231\210\346\200\235\345\223\262/20260316-hug\357\274\237.md" similarity index 100% rename from "\351\231\210\346\200\235\345\223\262/202603116-hug\357\274\237.md" rename to "\351\231\210\346\200\235\345\223\262/20260316-hug\357\274\237.md" diff --git "a/\351\231\210\346\200\235\345\223\262/202603118-HTTP\346\250\241\345\235\227.md" "b/\351\231\210\346\200\235\345\223\262/20260318-HTTP\346\250\241\345\235\227.md" similarity index 100% rename from "\351\231\210\346\200\235\345\223\262/202603118-HTTP\346\250\241\345\235\227.md" rename to "\351\231\210\346\200\235\345\223\262/20260318-HTTP\346\250\241\345\235\227.md" -- Gitee