3v324v23 commited on
Commit
89253be
·
1 Parent(s): 1b21241

feat: Add child_process demo and update README

Browse files
Files changed (2) hide show
  1. README.md +1 -0
  2. index.js +28 -0
README.md CHANGED
@@ -15,6 +15,7 @@ short_description: 演示 Node.js 大文件流式读写、XML/JSON 处理及性
15
  1. **大文件流式处理**:使用 Stream API 高效读写 GB 级文件,避免内存溢出。
16
  2. **XML 与 JSON 处理**:演示 `fast-xml-parser` 和 `stream-json` 的使用。
17
  3. **性能监控**:使用 `perf_hooks` 和 `process.memoryUsage` 实时监控执行时间和内存占用。
 
18
 
19
  ## 功能特性
20
 
 
15
  1. **大文件流式处理**:使用 Stream API 高效读写 GB 级文件,避免内存溢出。
16
  2. **XML 与 JSON 处理**:演示 `fast-xml-parser` 和 `stream-json` 的使用。
17
  3. **性能监控**:使用 `perf_hooks` 和 `process.memoryUsage` 实时监控执行时间和内存占用。
18
+ 4. **核心 API 演示**:全面展示 `fs` (文件系统), `path` (路径处理), `stream` (流), `child_process` (子进程) 等 Node.js 核心模块的应用。
19
 
20
  ## 功能特性
21
 
index.js CHANGED
@@ -5,6 +5,8 @@ const LargeFileHandler = require('./src/largeFile');
5
  const xmlJsonHandler = require('./src/xmlJson');
6
  const performanceMonitor = require('./src/performance');
7
 
 
 
8
  const app = express();
9
  const PORT = 7860;
10
 
@@ -130,17 +132,43 @@ app.get('/', async (req, res) => {
130
  body { font-family: monospace; background: #f0f0f0; padding: 20px; }
131
  pre { background: #fff; padding: 20px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); white-space: pre-wrap; }
132
  h1 { color: #333; }
 
133
  </style>
134
  </head>
135
  <body>
136
  <h1>Node.js Performance Demo Output</h1>
137
  <pre>${output}</pre>
138
  <p>Refresh to run again.</p>
 
139
  </body>
140
  </html>
141
  `);
142
  });
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  app.listen(PORT, () => {
145
  console.log(`Server is running on http://localhost:${PORT}`);
146
  });
 
5
  const xmlJsonHandler = require('./src/xmlJson');
6
  const performanceMonitor = require('./src/performance');
7
 
8
+ const { exec } = require('child_process');
9
+
10
  const app = express();
11
  const PORT = 7860;
12
 
 
132
  body { font-family: monospace; background: #f0f0f0; padding: 20px; }
133
  pre { background: #fff; padding: 20px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); white-space: pre-wrap; }
134
  h1 { color: #333; }
135
+ .btn { display: inline-block; padding: 10px 20px; background: #007bff; color: white; text-decoration: none; border-radius: 5px; margin-top: 20px; }
136
  </style>
137
  </head>
138
  <body>
139
  <h1>Node.js Performance Demo Output</h1>
140
  <pre>${output}</pre>
141
  <p>Refresh to run again.</p>
142
+ <a href="/system-info" class="btn">View System Info (child_process demo)</a>
143
  </body>
144
  </html>
145
  `);
146
  });
147
 
148
+ app.get('/system-info', (req, res) => {
149
+ exec('uname -a && echo "\\nDisk Usage:" && df -h | head -n 5', (error, stdout, stderr) => {
150
+ const output = error ? `Error: ${error.message}` : stdout;
151
+ res.send(`
152
+ <html>
153
+ <head>
154
+ <title>System Info</title>
155
+ <style>
156
+ body { font-family: monospace; background: #f0f0f0; padding: 20px; }
157
+ pre { background: #fff; padding: 20px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); white-space: pre-wrap; }
158
+ h1 { color: #333; }
159
+ .btn { display: inline-block; padding: 10px 20px; background: #007bff; color: white; text-decoration: none; border-radius: 5px; margin-top: 20px; }
160
+ </style>
161
+ </head>
162
+ <body>
163
+ <h1>System Info (via child_process)</h1>
164
+ <pre>${output}</pre>
165
+ <a href="/" class="btn">Back to Demo</a>
166
+ </body>
167
+ </html>
168
+ `);
169
+ });
170
+ });
171
+
172
  app.listen(PORT, () => {
173
  console.log(`Server is running on http://localhost:${PORT}`);
174
  });