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/202603013-\345\221\275\344\273\244\350\241\214\345\267\245\345\205\267.md" new file mode 100644 index 0000000000000000000000000000000000000000..7735ae98ec70544e7b446ecebea0eb8203b0bb8a --- /dev/null +++ "b/\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" @@ -0,0 +1,139 @@ +# 命令行工具 + +``` +import { rejects } from 'assert/strict'; +import { error } from 'console'; + +import { resolve } from 'dns'; + +import fs from 'fs'; + +let cmd = process.argv[2]; +let params = process.argv[3]; +let params2 = process.argv[4]; + +let filepath = './data.json' + +if (cmd == 'list') { + + console.log('列表:'); + + // const list = await readFile(); + + // const alist = list.map(item => `标题:${item.title}`).join('\n'); + + console.log(sortPriority()); + + // console.log(alist); +} +else if (cmd == 'add') { + + console.log('添加'); + + let data = readFile(); + + if (params2 != '高' || '中' || '低') { + error('优先级输入错误') + } + else { + data.push({ 'title': params, 'priority': params2 }); + + await writFile(data); + + console.log(readFile().map(item => `标题:${item.title}`).join('\n')); + + } +} +else if (cmd == 'del') { + + console.log('删除'); + + // 转成整数 + let idx = parseInt(params); + + let data = readFile(); + + data.splice(idx - 1, 1); + + writFile(data); + + console.log(readFile().map(item => `标题:${item.title}`).join('\n')); + + +} +else { + + console.log('未知命令'); + +} + +// 写文件 +function writFile(filecontent) { + + return new Promise((resolve, reject) => { + + let json = JSON.stringify(filecontent); + + fs.writeFile(filepath, json, 'utf-8', + (err) => { + if (err) { + reject(err); + } + else { + resolve(); + } + }) + }) +} + +// 读文件 +function readFile() { + + return new Promise((resolve, reject) => { + + if (fs.existsSync(filepath)) { + + fs.readFile(filepath, 'utf-8', (err, data) => { + if (err) { + console.log('读取文件失败:', err.message); + reject([]); + } + else { + try { + const result = data ? JSON.parse(data) : []; + resolve(result); + } + catch (err) { + console.log('解析失败:', err.message); + resolve([]); + } + } + }); + + } + else { + resolve([]); + } + + }) + +} + +// 优先级排序 +async function sortPriority() { + + const dataList = await readFile(); + + const priority = { + '高': 3, + '中': 2, + '低': 1 + } + + return dataList.sort((a, b) => { + return priority[b.priority] - priority[a.priority]; + }); + +} +``` + diff --git "a/\351\231\210\346\200\235\345\223\262/20260306-\345\210\235\350\257\206Node.js.md" "b/\351\231\210\346\200\235\345\223\262/20260306-\345\210\235\350\257\206Node.js.md" new file mode 100644 index 0000000000000000000000000000000000000000..ee5ddeae428fe0e1122da080f726140e0e6aa273 --- /dev/null +++ "b/\351\231\210\346\200\235\345\223\262/20260306-\345\210\235\350\257\206Node.js.md" @@ -0,0 +1,38 @@ +# 初识Node.js + +- ### Node.js是什么? + - **A: Node.js是一个基于Chrome V8引擎的JavaScript运行时,让JavaScript可以脱离浏览器在服务器端运行。** + +- ### Node.js有什么特点? + + - **单线程、事件驱动、非阻塞I/O、高性能。** + +*** + +## 练习 + +- 选择题:B,B,C,B,A + +### 简答题 + +1. 什么是Node.js?请用一句话概括。 + + - Node.js 是一个基于 Chrome V8 引擎的开源、跨平台 JavaScript 运行时环境,允许开发者在服务器端执行 JavaScript 代码。 + +2. 请解释"非阻塞I/O"是什么意思,它有什么优点? + + - 程序发起 I/O 操作(如文件读写、网络请求)后,无需等待该操作完成,可立即继续执行后续代码;当 I/O 操作完成时,系统会通过回调、事件等方式通知程序处理结果。与之相对的是 “阻塞 I/O”,程序会卡在 I/O 操作步骤,直到操作结束才能继续。高性能,高并发,轻量高效 + +3. 为什么Node.js适合开发实时聊天应用? + + - 非阻塞异步I/o + + - **原生支持 WebSocket**:Node.js 的`ws`等库可便捷实现客户端与服务器的双向实时通信,无需频繁轮询,降低服务器压力; + + **事件驱动模型** + +4. Node.js和传统后端语言(如Java、PHP)相比,有什么优势? + + 并发处理效率更高,开发效率更高,轻量启动快,实时性更好 + + diff --git "a/\351\231\210\346\200\235\345\223\262/20260309-\346\220\255\345\273\272\345\274\200\345\217\221\347\216\257\345\242\203.md" "b/\351\231\210\346\200\235\345\223\262/20260309-\346\220\255\345\273\272\345\274\200\345\217\221\347\216\257\345\242\203.md" new file mode 100644 index 0000000000000000000000000000000000000000..b7f52feb7b3f6bf1030eccfb3b1c330dd920cb14 --- /dev/null +++ "b/\351\231\210\346\200\235\345\223\262/20260309-\346\220\255\345\273\272\345\274\200\345\217\221\347\216\257\345\242\203.md" @@ -0,0 +1,57 @@ +# 搭建开发环境 + +### 工具 + +- node.js + +- npm + +*** + + + +### 验证安装 + +``` +# 查看Node.js版本 +node -v + +# 查看npm版本 +npm -v + +# 同时查看两者 +node -v && npm -v +``` + +### npm基本命令 + +| 命令 | 说明 | +| ------------------------------- | ---------------------------- | +| `npm -v` | 查看npm版本 | +| `npm init -y(-y是创建默认项目)` | 初始化项目,创建package.json | +| `npm install <包名>` | 安装指定包 | +| `npm install` | 安装所有依赖 | +| `npm uninstall <包名>` | 卸载指定包 | +| `npm list` | 查看已安装的包 | + + + +### 运行项目 + +``` +node app.js(文件名) +``` + +*** + +## 练习 + +- 选择题:B,B,C,B, + + + +## 简答题 + +1. npm和Node.js是什么关系? + - **npm(Node Package Manager)** 是 Node.js 的**默认包管理工具** + diff --git "a/\351\231\210\346\200\235\345\223\262/20260311-npm\344\270\216\345\214\205\347\256\241\347\220\206.md" "b/\351\231\210\346\200\235\345\223\262/20260311-npm\344\270\216\345\214\205\347\256\241\347\220\206.md" new file mode 100644 index 0000000000000000000000000000000000000000..79fe3aea528058e9a4780f9a472939e2383f6202 --- /dev/null +++ "b/\351\231\210\346\200\235\345\223\262/20260311-npm\344\270\216\345\214\205\347\256\241\347\220\206.md" @@ -0,0 +1,235 @@ +# npm与包管理 + +### Node.js的三大包管理器 + +- npm + +- yarm 速度快、离线安装、确定性安装 + +- pnpm 节省磁盘空间、更快、更安全 + +*** + + + +``` +# 初始化项目 +$ npm init -y + +# 安装express框架 +$ npm install express + +# 安装开发依赖 +$ npm install --save-dev nodemon + +# 查看已安装的包 +$ npm list + +# 运行项目 +$ npm start +``` +package.json字段说明 +``` +{ + "name": "my-blog", // 项目名称(必须,不能有大写) + "version": "1.0.0", // 版本号(语义化版本) + "description": "我的博客系统", // 项目描述 + "main": "index.js", // 入口文件 + "scripts": { // 自定义脚本命令 + "start": "node index.js", + "dev": "nodemon index.js", + "test": "jest" + }, + "keywords": ["blog", "node"], // 关键词 + "author": "张三", // 作者 + "license": "MIT", // 许可证 + "dependencies": { // 生产依赖(项目运行需要) + "express": "^4.18.2", + "mysql2": "^3.6.0" + }, + "devDependencies": { // 开发依赖(仅开发时需要) + "nodemon": "^3.0.0", + "jest": "^29.0.0" + } +} +``` + +#### 语义化版本号(SemVer) + +版本号格式:`主版本.次版本.补丁版本` + +| 符号 | 含义 | 示例 | +| ------ | -------- | -------------------------- | +| `^` | 兼容更新 | ^4.18.0 → 安装4.x.x最新版 | +| `~` | 补丁更新 | ~4.18.0 → 安装4.18.x最新版 | +| `*` | 最新版本 | * → 安装最新版本 | +| 无符号 | 固定版本 | 4.18.0 → 只安装4.18.0 | + +### npm常用命令 + +#### 项目初始化 + +``` +# 交互式初始化(会询问项目信息) +npm init + +# 使用默认配置初始化(快速) +npm init -y + +# 指定项目信息初始化 +npm init --yes +``` + +#### 安装包 + +``` +# 安装到dependencies(生产环境需要) +npm install <包名> +npm i <包名> + +# 安装到devDependencies(开发环境需要) +npm install --save-dev <包名> +npm i -D <包名> + +# 全局安装(所有项目可用) +npm install --global <包名> +npm i -g <包名> + +# 安装指定版本 +npm install express@4.18.0 + +# 安装多个包 +npm install express mysql2 cors +``` + +#### 管理和查看 + +``` +# 查看已安装的包 +npm list + +# 查看已安装的包(显示顶层) +npm list --depth=0 + +# 查看某个包的版本信息 +npm view express version + +# 查看某个包的所有版本 +npm view express versions + +# 查看包的信息 +npm view express +``` + +#### 更新和卸载 + +``` +# 更新包 +npm update <包名> + +# 更新所有依赖 +npm update + +# 卸载包 +npm uninstall <包名> +npm remove <包名> +``` + +#### npm脚本 + +``` +# 运行package.json中定义的脚本 +npm run <脚本名> + +# 常用简写 +npm start # npm run start +npm test # npm run test +npm stop # npm run stop +``` + +### Yarn与pnpm + +#### Yarn + +``` +# 安装Yarn +npm install -g yarn + +# 初始化项目 +yarn init + +# 安装依赖 +yarn add <包名> # 生产依赖 +yarn add <包名> --dev # 开发依赖 +yarn add <包名> -D # 简写 + +# 全局安装 +yarn global add <包名> + +# 卸载 +yarn remove <包名> + +# 运行脚本 +yarn <脚本名> +yarn start + +# 查看依赖 +yarn list +yarn outdated +``` + +#### pnpm + +``` +# 安装pnpm +npm install -g pnpm + +# 初始化项目 +pnpm init + +# 安装依赖 +pnpm add <包名> # 生产依赖 +pnpm add -D <包名> # 开发依赖 + +# 全局安装 +pnpm add -g <包名> + +# 卸载 +pnpm remove <包名> + +# 运行脚本 +pnpm <脚本名> +pnpm start + +# 查看依赖 +pnpm list +pnpm outdated +``` + +安装express框架: + +``` +npm install express +``` + +安装nodemon作为开发依赖: + +``` +npm install --save-dev nodemon +``` + +*** + +## 练习 + 选择题:B,D,B,C/D,B,B,B + + dependencies 和 devDependencies 的核心区别 + +| 维度 | dependencies(生产依赖) | devDependencies(开发依赖) | +| :--------------: | :----------------------------------------------: | :-----------------------------------------------: | +| **核心作用** | 项目**运行时必须**的依赖,缺少则项目无法正常工作 | 仅**开发 / 构建阶段**需要的依赖,运行时完全不需要 | +| **使用场景** | 业务核心功能依赖(如后端框架、请求库、UI 组件) | 开发辅助工具(如打包、测试、代码检查工具) | +| **典型示例** | express、axios、react、vue、lodash | webpack、eslint、jest、babel、sass-loader | +| **安装命令** | `npm install 包名`(默认)`npm i 包名 -S` | `npm install 包名 --save-dev``npm i 包名 -D` | +| **生产环境安装** | `npm install --production` 会安装 | `npm install --production` 会**忽略** | +| **是否影响运行** | 影响(缺少则项目报错) | 不影响(仅影响开发 / 构建) | diff --git "a/\351\231\210\346\200\235\345\223\262/20260312-\346\250\241\345\235\227\347\263\273\347\273\237.md" "b/\351\231\210\346\200\235\345\223\262/20260312-\346\250\241\345\235\227\347\263\273\347\273\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..3c713f8f238bf1bf6c0e4ae45eb484b278ccb722 --- /dev/null +++ "b/\351\231\210\346\200\235\345\223\262/20260312-\346\250\241\345\235\227\347\263\273\347\273\237.md" @@ -0,0 +1,79 @@ +# 模块系统 + +## 两种模块系统的区别 +| 特性 | CommonJS | ES Modules | +| -------- | ---------------- | ---------------- | +| 引入方式 | `require()` | `import` | +| 导出方式 | `module.exports` | `export` | +| 加载方式 | 同步加载 | 异步加载 | +| 解析时机 | 运行时解析 | 编译时确定 | +| 使用场景 | Node.js服务端 | 前端/现代Node.js | +| 扩展名 | `.js` | `.mjs` 或配置 | + +*** +### 使用概览 +a.js文件(模块) + +``` +// Common +//语法module.exports +// module.exports={con:()=>console.log('123')} + + +// ESM +//语法export +// export let con =()=>console.log('123'); +``` +main.js文件(主文件) +``` +// Common导入使用的三种方法 +//1. +const {con} = require('./a'); +con(); +//2. + const a =require('./a'); + console.log(a.con()); +//3. +// const { con as cons} =require('./a'); +// cons(); + +// ESM导入使用的方法 +//1. +// import {con} from './a.js'; +// console.log(con()); + +``` + +*** + +## 练习 + +- 选择题:A,C,B,B,B + + 1. 什么是模块?为什么要使用模块? + + 增加代码复用性,避免命名冲突,提升维护性 + + 2. 请解释module.exports和exports的区别。 + +`exports` 是 `module.exports` 的**引用(快捷方式)**,Node.js 初始化模块时会执行 `exports = module.exports = {}`。 + + 3. CommonJS和ES Modules有什么区别? + + | 维度 | CommonJS(Node.js 默认) | ES Modules(ES6 模块化) | + | -------- | ------------------------------------------------------ | --------------------------------------------------- | + | 语法 | `require()` 导入,`module.exports`/`exports` 导出 | `import/export` 导入导出 | + | 加载时机 | 运行时加载(动态加载,可写在代码块中) | 编译时加载(静态加载,必须写在顶层) | + | 作用域 | 模块是对象,加载的是整个模块的拷贝 | 模块是字符串,加载的是引用,值可实时更新 | + | 文件后缀 | 默认 `.js` | 需要 `.mjs` 或配置 `package.json` 的 `type: module` | + | 路径要求 | `require` 可省略后缀(如 `require('./a')`) | `import` 必须写完整后缀(如 `import './a.js'`) | + | 示例 | `const fs = require('fs')` `module.exports = {foo:1}` | `import fs from 'fs'` `export const foo = 1` | + + + 4. Node.js加载模块的顺序是怎样的? + + Node.js 加载模块遵循 “先缓存 → 再分类加载” 的顺序,核心步骤 + + 5. 什么是模块作用域?它有什么作用? + + **模块作用域**:每个 Node.js 模块都有独立的作用域,模块内定义的变量、函数、类默认仅在该模块内可见,不会暴露到全局作用域。