From d25ab7609992938c13c4a7ddc8bfb8be91790e32 Mon Sep 17 00:00:00 2001 From: yangmao <546016136@qq.com> Date: Sun, 22 Mar 2026 19:11:05 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=94=E8=AE=B0+=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20260316-\344\275\234\344\270\232.md" | 135 ++++++++++++++++++ ...05\347\275\256\346\250\241\345\235\227.md" | 8 ++ ...73\347\273\237\346\250\241\345\235\227.md" | 33 +++++ .../20260320-http\346\250\241\345\235\227.md" | 22 +++ 4 files changed, 198 insertions(+) create mode 100644 "\345\274\240\346\264\213\351\223\255/20260316-\344\275\234\344\270\232.md" create mode 100644 "\345\274\240\346\264\213\351\223\255/20260318-\345\237\272\346\234\254\345\206\205\347\275\256\346\250\241\345\235\227.md" create mode 100644 "\345\274\240\346\264\213\351\223\255/20260319-\346\226\207\344\273\266\347\263\273\347\273\237\346\250\241\345\235\227.md" create mode 100644 "\345\274\240\346\264\213\351\223\255/20260320-http\346\250\241\345\235\227.md" diff --git "a/\345\274\240\346\264\213\351\223\255/20260316-\344\275\234\344\270\232.md" "b/\345\274\240\346\264\213\351\223\255/20260316-\344\275\234\344\270\232.md" new file mode 100644 index 00000000..8e3d4dde --- /dev/null +++ "b/\345\274\240\346\264\213\351\223\255/20260316-\344\275\234\344\270\232.md" @@ -0,0 +1,135 @@ +## 作业 + + + + +### 操作题 + +1.信息展示:创建一个程序,输出当前系统的所有环境变量。 +```js +console.log(process.env); +``` +2.参数解析:创建一个计算器程序,支持: +node calc.js add 5 3 → 输出 8 +node calc.js sub 5 3 → 输出 2 +node calc.js mul 5 3 → 输出 15 + +```js +import { log } from 'console'; +import fs from 'fs'; + +let a = process.argv[2]; +let b = process.argv[3]; +let c = process.argv[4]; + +if (a == 'add') { + let sum = b * 1 + c * 1; + log(sum); +} else if (a == 'sub') { + let sum = b * 1 - c * 1; + log(sum); +} else if (a == 'mul') { + let sum = b * 1 * c * 1; + log(sum); +} else { + log('错误'); +} +``` + +3.Buffer操作:创建一个程序,将中文字符串转为Buffer,再转回字符串,验证是否一致。 +```js +import fs from 'fs'; +let buf1 = Buffer.from('羊羊毛'); +console.log(buf1); +console.log(buf1.toString()); +``` + +4.路径处理:创建一个程序,接收文件路径,输出文件名、扩展名、所在目录。 + +```js +import { log } from 'console'; +import path from 'path'; + +let filePath = './张洋铭\text\app.js'; + +path.basename(filePath); +path.basename(filePath, '.js'); + +// 获取扩展名 +path.extname(filePath); + +// 获取目录 +path.dirname(filePath); + +// 解析路径信息 + + +log(path.parse(filePath)) +``` + + + + + +1.文件读取器:创建一个程序,读取指定文件的内容并输出到控制台。 +```js +const fs = require('fs'); + +// 读取文件(同步) +const content = fs.readFileSync('./data.txt', 'utf8'); +console.log(content); +``` +2.文件写入器:创建一个程序,接收命令行输入的内容,写入到文件中。 + +```js +import fs from 'fs'; + +fs.appendFileSync('./aaa.text', 'holleword') + +``` + + + + +1.静态服务器:创建一个Web服务器,返回HTML页面,包含CSS样式。 +```js +const http = require('http'); +const fs = require('fs'); +const path = require('path'); +const server = http.createServer((req, res) => { + + let filePath = '.' + req.url; + if (filePath === './') { + filePath = './index.html'; + } + +const extname = String(path.extname(filePath)).toLowerCase(); + const contentType = mimeTypes[extname] || 'application/octet-stream'; + + fs.readFile(filePath, (error, content) => { + if (error) { + if (error.code === 'ENOENT') { + + fs.readFile('./404.html', (err, content404) => { + res.writeHead(404, { 'Content-Type': 'text/html' }); + res.end(content404, 'utf-8'); + }); + } else { + + res.writeHead(500); + res.end(`Server Error: ${error.code}`); + } + } else { + + res.writeHead(200, { 'Content-Type': contentType }); + res.end(content, 'utf-8'); + } + }); +}); + + +const PORT = process.env.PORT || 3000; +server.listen(PORT, () => { + console.log(`Server running at http://localhost:${PORT}/`); +}); +``` diff --git "a/\345\274\240\346\264\213\351\223\255/20260318-\345\237\272\346\234\254\345\206\205\347\275\256\346\250\241\345\235\227.md" "b/\345\274\240\346\264\213\351\223\255/20260318-\345\237\272\346\234\254\345\206\205\347\275\256\346\250\241\345\235\227.md" new file mode 100644 index 00000000..11a31dea --- /dev/null +++ "b/\345\274\240\346\264\213\351\223\255/20260318-\345\237\272\346\234\254\345\206\205\347\275\256\346\250\241\345\235\227.md" @@ -0,0 +1,8 @@ + +## 笔记 +|知识点|关键内容| +|:-----|-----:| +|console|log/info/warn/error/table/count/time/trace| +|process|version/platform/cwd()/argv/exit/on| +|Buffer|from/alloc/toString/allocUnsafe/concat| +|path|join/resolve/basename/extname/dirname| \ No newline at end of file diff --git "a/\345\274\240\346\264\213\351\223\255/20260319-\346\226\207\344\273\266\347\263\273\347\273\237\346\250\241\345\235\227.md" "b/\345\274\240\346\264\213\351\223\255/20260319-\346\226\207\344\273\266\347\263\273\347\273\237\346\250\241\345\235\227.md" new file mode 100644 index 00000000..b6f09a4d --- /dev/null +++ "b/\345\274\240\346\264\213\351\223\255/20260319-\346\226\207\344\273\266\347\263\273\347\273\237\346\250\241\345\235\227.md" @@ -0,0 +1,33 @@ +## 笔记 + +|知识点|关键内容| +|:-----|-----:| +|读取文件|readFileSync / readFile / promises.readFile| +|写入文件|writeFileSync / appendFileSync / writeFile| +|文件操作|existsSync / stat / unlink / rename / copyFile| +|目录操作|mkdir / rmdir / readdir| +|文件监听|watch / watchFile| + +### 常用方法 +```js +const fs = require('fs'); + +// 读取 +fs.readFileSync(path, 'utf8'); +fs.readFile(path, 'utf8', callback); + +// 写入 +fs.writeFileSync(path, data); +fs.appendFileSync(path, data); + +// 检查 +fs.existsSync(path); +fs.statSync(path); + +// 操作 +fs.mkdirSync(path, { recursive: true }); +fs.readdirSync(path); +fs.unlinkSync(path); +fs.renameSync(oldPath, newPath); +fs.copyFileSync(src, dest); +``` \ No newline at end of file diff --git "a/\345\274\240\346\264\213\351\223\255/20260320-http\346\250\241\345\235\227.md" "b/\345\274\240\346\264\213\351\223\255/20260320-http\346\250\241\345\235\227.md" new file mode 100644 index 00000000..fef82b0c --- /dev/null +++ "b/\345\274\240\346\264\213\351\223\255/20260320-http\346\250\241\345\235\227.md" @@ -0,0 +1,22 @@ + +## 笔记 +### 步骤一:创建简单的Web服务器 +- 创建项目文件夹:http-demo +- 初始化项目 +- 创建 server.js +- 启动服务器:node 文件名 + +### 步骤二:创建RESTful API +- 创建 api-server.js +- 启动并测试node +- 使用curl测试 + +### 步骤三:创建HTTP客户端 +- 创建 client.js +- 确保API服务器运行,然后测试node + +|知识点|关键内容| +|:-----|-----:| +|创建服务器|http.createServer()| +|请求对象|req.method / req.url / req.headers| +|响应对象|res.writeHead / res.end / res.setHeader| -- Gitee