diff --git "a/\346\234\261\346\203\240\346\203\240/20260316-\344\275\234\344\270\232.md" "b/\346\234\261\346\203\240\346\203\240/20260316-\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..83e07c40d3cff2625a8bb92d59f7c1cdda855348 --- /dev/null +++ "b/\346\234\261\346\203\240\346\203\240/20260316-\344\275\234\344\270\232.md" @@ -0,0 +1,44 @@ +## 笔记 +```js +import fs ,{read} from 'fs' +import { type } from 'os'; + +const Address='./text.json'; + +let command=process.argv[2]; +let params=process.argv[3]; + +if(command=='add'){ + let arr=readFromJson(); + arr.push({ + amount:params*1, + type:'input', + timestamp:new Date() + }) + writeToJson(arr); + printAmount(); +} +else if(command=='del'){ + +} +else if(){} +else if(){} + +function readFromJson(){ + if(fs.existsSync(Address)){ + let jsonString=fs.readFileSync(Address,'utf-8'); + if(jsonString.length>0){ + return JSON.parse(jsonString); + + } + return []; + } + return []; +} + +function writeToJson(arr){ + let jsonString=JSON.stringify(arr); + fs.writeFileSync(Address,jsonString); +} + +``` \ No newline at end of file diff --git "a/\346\234\261\346\203\240\346\203\240/20260318-\344\275\234\344\270\232.md" "b/\346\234\261\346\203\240\346\203\240/20260318-\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..94b748712138f98092ccd92c41e89eab441542d8 --- /dev/null +++ "b/\346\234\261\346\203\240\346\203\240/20260318-\344\275\234\344\270\232.md" @@ -0,0 +1,146 @@ +## 笔记 + +### console 模块 +1. 常用方法 +- `console.log()` 普通输出 +- `console.info()` 提示 +- `console.warn()` 警告 +- `console.error()` 错误 + +2. 高级用法 +- `console.table()` 表格展示数组/对象 +- `console.time()` / `timeEnd()` 代码计时 +- `console.count()` 计数 / `countReset()` 重置 +- `console.assert(条件, 提示)` 断言,条件为假时输出 +- `console.trace()` 打印调用栈 + +3. 格式化 +- `%s` 字符串、`%d` 数字、`%j` JSON + +### process 模块 +1. 常用属性 +- `process.version` Node 版本 +- `process.platform` 系统平台(win32/darwin/linux) +- `process.arch` 系统架构(x64/arm64) +- `process.cwd()` 当前**命令行工作目录** +- `process.env` 环境变量对象 + +2. 命令行参数 process.argv +- 数组结构:`[node路径, 文件路径, 参数1, 参数2, ...]` +- 取参数:`const args = process.argv.slice(2)` + +3. 进程控制 +- `process.exit(0)` 正常退出;`exit(1)` 异常退出 +- `process.on('exit', 回调)` 监听退出 +- `process.on('SIGINT', 回调)` 监听 Ctrl+C +- `process.on('uncaughtException', 回调)` 捕获未处理异常 + +### Buffer 模块 +1. 作用 +处理文件、网络、图片等二进制数据 + +2. 常用 API +- `Buffer.from(str)` 字符串转 Buffer +- `Buffer.alloc(size)` 创建指定长度、清零的 Buffer +- `Buffer.allocUnsafe(size)` 快速创建(可能含旧数据) +- `buf.toString()` Buffer 转回字符串 +- `Buffer.concat([buf1, buf2])` 拼接多个 Buffer +- `buf.write(内容)` 向 Buffer 写入数据 +3. +- 一个中文占3 字节(UTF-8) + +### path 模块 +1. 核心区别 +- `__dirname`:当前**文件所在目录**(固定) +- `process.cwd()`:当前**命令行执行目录**(可变) +- `__filename`:当前文件完整路径 + +## 作业 +1. +``` +console.log('**系统所有的环境变量**'); +console.log(process.env); +``` + +2. +```js +console.log('**系统所有的环境变量**'); +console.log(process.env); + +let command = process.argv[2]; +let num1=Number(process.argv[3]); +let num2=Number(process.argv[4]); + +if (command == 'add') { + console.log(num1 + num2); +}else if(command == 'sub'){ + console.log(num1-num2); +}else if(command=='mul'){ + console.log(num1*num2); +}else{ + console.log('非法输入,请重试'); +} +console.log('请选择以下输入'); +console.log('node calc.js add 5 3'); +console.log('node calc.js sub 5 3'); +console.log('node calc.js mul 5 3'); +``` + +3. +```js +let str = 'Hello,world!'; +let BufferStr = Buffer.from(str); +console.log('转换成buffter:',BufferStr); +console.log('转回字符串',BufferStr.toString()); +console.log(str==BufferStr.toString()); +``` + +4. +```js +const path= require('path'); +const filePath = process.argv[2]; +console.log('文件名',path.basename(filePath)); +console.log('拓展名',path.extname(filePath)); +console.log('所在目录',path.dirname(filePath)); +``` + +5. +```js +import fs, { read } from 'fs' +let command = process.argv[2]; +let params = process.argv[3]; + +if (command == 'list') { + console.log('待办列表'); + let list = readFile(); + console.log(list); + +} else if (command == 'add') { + console.log('添加事项'); + let arr = readFile(); + arr.push({ 'title': params, 'isDone': false }); + writeFile(arr); + let newArr = readFile(); + console.log(newArr); +} else if (command == 'del') { + console.log('删除事项'); + let idx = parseInt(params); + let delArr = readFile(); + delArr.splice(idx - 1, 1); + writeFile(delArr); +}else { + console.log('未知命令,请确认重试'); +} +function readFile(filePath) { + filePath = filePath || './todos.json'; + if (fs.existsSync(filePath)) { + let data = fs.readFileSync(filePath, 'utf-8'); + return JSON.parse(data) || []; + } return []; +} +function writeFile(fileContent, filePath) { + filePath = filePath || './todos.json'; + let jsonString = JSON.stringify(fileContent); + fs.writeFileSync(filePath, jsonString); +} +``` \ No newline at end of file diff --git "a/\346\234\261\346\203\240\346\203\240/20260319-\344\275\234\344\270\232.md" "b/\346\234\261\346\203\240\346\203\240/20260319-\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..9e58cd32c80154676266ccc6f8d389ff6f9d8eb1 --- /dev/null +++ "b/\346\234\261\346\203\240\346\203\240/20260319-\344\275\234\344\270\232.md" @@ -0,0 +1,68 @@ +## 笔记 +## 文件系统 +### fs模块 +- 特点:功能完善、两种方式(同步(带Sync)和异步(回调/Promise))、流式处理、权限控制 +#### 读取文件 + - 同步读取 + + - 异步读取 + - 回调方式 + - Promise方式 + +#### 写入文件 + - 同步写入 + ```js + fs.writeFileSync('./data.txt', 'Hello World'); + ``` + + + - 异步写入 + ```js + fs.writeFile('./data.txt', 'Hello World', 'utf8', (err) => { + if (err) throw err; + console.log('写入成功'); + }); + ``` + ## 作业 + 1. +```js +import fs from 'fs' + +let files = fs.readFileSync('./read1.js','utf-8'); +console.log(files); +``` +2. +```js +import fs from 'fs'; + +let filePath = process.argv[2]; +let content = process.argv[3]; + +fs.writeFileSync(filePath,content,'utf-8'); +console.log('完成写入'); +``` +4. +```js +import fs from 'fs'; +import path from 'path'; + +function findFiles(dir, ext) { + if (!fs.existsSync(dir)) return; + + let files = fs.readdirSync(dir); + + files.forEach(file => { + let fullPath = path.join(dir, file); + let stat = fs.statSync(fullPath); + + if (stat.isDirectory()) { + findFiles(fullPath, ext); + } else { + if (file.endsWith(ext)) { + console.log(fullPath); + } + } + }); +} +findFiles('./', '.js'); +``` \ No newline at end of file diff --git "a/\346\234\261\346\203\240\346\203\240/20260320-\344\275\234\344\270\232.md" "b/\346\234\261\346\203\240\346\203\240/20260320-\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..7549faac1ced3a3363f0f097375b01f68e95b43f --- /dev/null +++ "b/\346\234\261\346\203\240\346\203\240/20260320-\344\275\234\344\270\232.md" @@ -0,0 +1,36 @@ +## 笔记 +### 定义 +- HTTP 协议:浏览器 ⬅➡ 服务器通信规则,请求 → 处理 → 响应 +- http 模块:Node.js 内置模块,用于创建 Web 服务器、发起 HTTP 请求 +### 创建服务器 +1. 基础模板 +```js +const http = require('http'); +const server = http.createServer((req, res) => { + // req:请求对象 res:响应对象 + res.writeHead(200, { 'Content-Type': 'text/html;charset=utf-8' }); + res.end('