feat: Add Core API demonstration module
Browse files- index.js +14 -1
- src/coreApiDemo.js +104 -0
index.js
CHANGED
|
@@ -5,6 +5,7 @@ const { exec } = require('child_process');
|
|
| 5 |
const LargeFileHandler = require('./src/largeFile');
|
| 6 |
const xmlJsonHandler = require('./src/xmlJson');
|
| 7 |
const performanceMonitor = require('./src/performance');
|
|
|
|
| 8 |
|
| 9 |
const app = express();
|
| 10 |
const PORT = 7860;
|
|
@@ -68,6 +69,12 @@ async function runJsonDemo() {
|
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
function logMemoryUsage() {
|
| 72 |
const used = process.memoryUsage();
|
| 73 |
console.log('Memory Usage:');
|
|
@@ -92,9 +99,11 @@ app.get('/', (req, res) => {
|
|
| 92 |
button { padding: 15px; font-size: 16px; cursor: pointer; background: #007bff; color: white; border: none; border-radius: 5px; transition: background 0.3s; font-weight: bold; }
|
| 93 |
button:hover { background: #0056b3; }
|
| 94 |
button:disabled { background: #ccc; cursor: not-allowed; }
|
|
|
|
|
|
|
| 95 |
.btn-system { background: #28a745; }
|
| 96 |
.btn-system:hover { background: #218838; }
|
| 97 |
-
.btn-clear { background: #6c757d;
|
| 98 |
.btn-clear:hover { background: #5a6268; }
|
| 99 |
#output { background: #1e1e1e; color: #d4d4d4; padding: 20px; border-radius: 5px; font-family: "Menlo", "Monaco", "Courier New", monospace; height: 500px; overflow-y: auto; white-space: pre-wrap; font-size: 14px; line-height: 1.5; border: 1px solid #333; }
|
| 100 |
.status { margin-top: 10px; font-style: italic; color: #666; }
|
|
@@ -108,6 +117,7 @@ app.get('/', (req, res) => {
|
|
| 108 |
<button onclick="runDemo('xml')">🏷️ 2. XML 解析处理</button>
|
| 109 |
<button onclick="runDemo('json')">📦 3. JSON 流式解析</button>
|
| 110 |
<button onclick="getSystemInfo()" class="btn-system">🖥️ 4. 系统信息 (child_process)</button>
|
|
|
|
| 111 |
<button onclick="clearOutput()" class="btn-clear">🗑 清空输出</button>
|
| 112 |
</div>
|
| 113 |
<div id="output">点击上方按钮开始测试...</div>
|
|
@@ -251,6 +261,9 @@ app.get('/api/run-demo', async (req, res) => {
|
|
| 251 |
case 'json':
|
| 252 |
await runJsonDemo();
|
| 253 |
break;
|
|
|
|
|
|
|
|
|
|
| 254 |
default:
|
| 255 |
console.log('未知的任务类型');
|
| 256 |
}
|
|
|
|
| 5 |
const LargeFileHandler = require('./src/largeFile');
|
| 6 |
const xmlJsonHandler = require('./src/xmlJson');
|
| 7 |
const performanceMonitor = require('./src/performance');
|
| 8 |
+
const CoreApiDemo = require('./src/coreApiDemo');
|
| 9 |
|
| 10 |
const app = express();
|
| 11 |
const PORT = 7860;
|
|
|
|
| 69 |
}
|
| 70 |
}
|
| 71 |
|
| 72 |
+
// Feature 4: Core APIs Demo
|
| 73 |
+
async function runCoreApiDemo() {
|
| 74 |
+
const coreDemo = new CoreApiDemo(DATA_DIR);
|
| 75 |
+
await coreDemo.run();
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
function logMemoryUsage() {
|
| 79 |
const used = process.memoryUsage();
|
| 80 |
console.log('Memory Usage:');
|
|
|
|
| 99 |
button { padding: 15px; font-size: 16px; cursor: pointer; background: #007bff; color: white; border: none; border-radius: 5px; transition: background 0.3s; font-weight: bold; }
|
| 100 |
button:hover { background: #0056b3; }
|
| 101 |
button:disabled { background: #ccc; cursor: not-allowed; }
|
| 102 |
+
.btn-core { background: #6610f2; }
|
| 103 |
+
.btn-core:hover { background: #520dc2; }
|
| 104 |
.btn-system { background: #28a745; }
|
| 105 |
.btn-system:hover { background: #218838; }
|
| 106 |
+
.btn-clear { background: #6c757d; }
|
| 107 |
.btn-clear:hover { background: #5a6268; }
|
| 108 |
#output { background: #1e1e1e; color: #d4d4d4; padding: 20px; border-radius: 5px; font-family: "Menlo", "Monaco", "Courier New", monospace; height: 500px; overflow-y: auto; white-space: pre-wrap; font-size: 14px; line-height: 1.5; border: 1px solid #333; }
|
| 109 |
.status { margin-top: 10px; font-style: italic; color: #666; }
|
|
|
|
| 117 |
<button onclick="runDemo('xml')">🏷️ 2. XML 解析处理</button>
|
| 118 |
<button onclick="runDemo('json')">📦 3. JSON 流式解析</button>
|
| 119 |
<button onclick="getSystemInfo()" class="btn-system">🖥️ 4. 系统信息 (child_process)</button>
|
| 120 |
+
<button onclick="runDemo('core-api')" class="btn-core">🛠️ 5. 核心 API 综合演示</button>
|
| 121 |
<button onclick="clearOutput()" class="btn-clear">🗑 清空输出</button>
|
| 122 |
</div>
|
| 123 |
<div id="output">点击上方按钮开始测试...</div>
|
|
|
|
| 261 |
case 'json':
|
| 262 |
await runJsonDemo();
|
| 263 |
break;
|
| 264 |
+
case 'core-api':
|
| 265 |
+
await runCoreApiDemo();
|
| 266 |
+
break;
|
| 267 |
default:
|
| 268 |
console.log('未知的任务类型');
|
| 269 |
}
|
src/coreApiDemo.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const fs = require('fs');
|
| 2 |
+
const path = require('path');
|
| 3 |
+
const os = require('os');
|
| 4 |
+
const EventEmitter = require('events');
|
| 5 |
+
const { pipeline } = require('stream');
|
| 6 |
+
const { promisify } = require('util');
|
| 7 |
+
|
| 8 |
+
const pipelineAsync = promisify(pipeline);
|
| 9 |
+
|
| 10 |
+
/**
|
| 11 |
+
* Node.js 核心 API 演示模块
|
| 12 |
+
* 展示: fs, path, stream, os, events, util
|
| 13 |
+
*/
|
| 14 |
+
class CoreApiDemo extends EventEmitter {
|
| 15 |
+
constructor(dataDir) {
|
| 16 |
+
super();
|
| 17 |
+
this.dataDir = dataDir;
|
| 18 |
+
this.demoFile = path.join(dataDir, 'core_demo.txt');
|
| 19 |
+
this.copyFile = path.join(dataDir, 'core_demo_copy.txt');
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
async run() {
|
| 23 |
+
console.log('--- 开始 Node.js 核心 API 演示 ---');
|
| 24 |
+
|
| 25 |
+
try {
|
| 26 |
+
// 1. Path 模块演示
|
| 27 |
+
this.demoPath();
|
| 28 |
+
|
| 29 |
+
// 2. OS 模块演示
|
| 30 |
+
this.demoOs();
|
| 31 |
+
|
| 32 |
+
// 3. Events 模块演示
|
| 33 |
+
this.demoEvents();
|
| 34 |
+
|
| 35 |
+
// 4. FS & Stream 模块演示
|
| 36 |
+
await this.demoFsAndStream();
|
| 37 |
+
|
| 38 |
+
} catch (err) {
|
| 39 |
+
console.error('核心 API 演示出错:', err);
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
console.log('--- 核心 API 演示结束 ---');
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
demoPath() {
|
| 46 |
+
console.log('\n[Path 模块]');
|
| 47 |
+
console.log(`__dirname: ${__dirname}`);
|
| 48 |
+
console.log(`文件名 (basename): ${path.basename(this.demoFile)}`);
|
| 49 |
+
console.log(`扩展名 (extname): ${path.extname(this.demoFile)}`);
|
| 50 |
+
console.log(`解析路径 (parse):`, path.parse(this.demoFile));
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
demoOs() {
|
| 54 |
+
console.log('\n[OS 模块]');
|
| 55 |
+
console.log(`平台: ${os.platform()} (${os.arch()})`);
|
| 56 |
+
console.log(`主机名: ${os.hostname()}`);
|
| 57 |
+
console.log(`总内存: ${(os.totalmem() / 1024 / 1024 / 1024).toFixed(2)} GB`);
|
| 58 |
+
console.log(`空闲内存: ${(os.freemem() / 1024 / 1024 / 1024).toFixed(2)} GB`);
|
| 59 |
+
console.log(`CPU核数: ${os.cpus().length}`);
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
demoEvents() {
|
| 63 |
+
console.log('\n[Events 模块]');
|
| 64 |
+
console.log('注册自定义事件 "hello"...');
|
| 65 |
+
|
| 66 |
+
// 监听一次性事件
|
| 67 |
+
this.once('hello', (data) => {
|
| 68 |
+
console.log(`触发了 "hello" 事件,数据: ${JSON.stringify(data)}`);
|
| 69 |
+
});
|
| 70 |
+
|
| 71 |
+
// 触发事件
|
| 72 |
+
this.emit('hello', { msg: 'Node.js is cool!', timestamp: Date.now() });
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
async demoFsAndStream() {
|
| 76 |
+
console.log('\n[FS & Stream 模块]');
|
| 77 |
+
|
| 78 |
+
// FS: 写入文件
|
| 79 |
+
const content = 'Hello Node.js Core APIs!\nThis is a test file for streams.\n' + new Date().toISOString();
|
| 80 |
+
console.log(`写入文件: ${this.demoFile}`);
|
| 81 |
+
fs.writeFileSync(this.demoFile, content);
|
| 82 |
+
|
| 83 |
+
// FS: 获取文件状态
|
| 84 |
+
const stats = fs.statSync(this.demoFile);
|
| 85 |
+
console.log(`文件大小: ${stats.size} bytes`);
|
| 86 |
+
console.log(`创建时间: ${stats.birthtime}`);
|
| 87 |
+
|
| 88 |
+
// Stream: 流式复制文件
|
| 89 |
+
console.log(`使用 Stream Pipeline 复制文件到: ${this.copyFile}`);
|
| 90 |
+
|
| 91 |
+
await pipelineAsync(
|
| 92 |
+
fs.createReadStream(this.demoFile),
|
| 93 |
+
fs.createWriteStream(this.copyFile)
|
| 94 |
+
);
|
| 95 |
+
|
| 96 |
+
console.log('文件复制完成 (Stream pipeline)');
|
| 97 |
+
|
| 98 |
+
// FS: 读取复制后的文件验证
|
| 99 |
+
const copiedContent = fs.readFileSync(this.copyFile, 'utf8');
|
| 100 |
+
console.log('读取复制文件内容前 50 字符:', copiedContent.substring(0, 50).replace(/\n/g, '\\n') + '...');
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
module.exports = CoreApiDemo;
|