import fs from 'node:fs';
import http from 'node:http';
import inspector from 'node:inspector';
import os from 'node:os';
import path from 'node:path';
const cases = [];
// Case 1: project_init_and_package_json
{
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nodejs-ch04-proj-'));
try {
const projectDir = path.join(tmpDir, 'my-node-app');
fs.mkdirSync(projectDir, { recursive: true });
const manifest = {
name: 'my-node-app',
version: '1.0.0',
description: '',
main: 'app.js',
scripts: { test: 'echo "Error: no test specified" && exit 1' },
keywords: [],
author: '',
license: 'ISC',
};
const pkgPath = path.join(projectDir, 'package.json');
fs.writeFileSync(pkgPath, JSON.stringify(manifest, null, 2), 'utf8');
const parsed = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
const actual = {
dirExists: fs.existsSync(projectDir),
name: parsed.name,
version: parsed.version,
main: parsed.main,
license: parsed.license,
};
const expected = {
dirExists: true,
name: 'my-node-app',
version: '1.0.0',
main: 'app.js',
license: 'ISC',
};
cases.push({
name: 'project_init_and_package_json',
passed: JSON.stringify(actual) === JSON.stringify(expected),
expected,
actual,
});
} finally {
fs.rmSync(tmpDir, { recursive: true, force: true });
}
}
// Case 2: http_app_js_loopback_server
{
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, Node.js!\n');
});
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
try {
const { port } = server.address();
const resp = await fetch(`http://127.0.0.1:${port}/`);
const actual = {
status: resp.status,
contentType: resp.headers.get('content-type'),
body: await resp.text(),
};
const expected = {
status: 200,
contentType: 'text/plain',
body: 'Hello, Node.js!\n',
};
cases.push({
name: 'http_app_js_loopback_server',
passed: JSON.stringify(actual) === JSON.stringify(expected),
expected,
actual,
});
} finally {
await new Promise((resolve) => server.close(resolve));
}
}
// Case 3: vscode_launch_json_and_inspector
{
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nodejs-ch04-dbg-'));
try {
const vscodeDir = path.join(tmpDir, '.vscode');
fs.mkdirSync(vscodeDir, { recursive: true });
const launchConfig = {
version: '0.2.0',
configurations: [
{
type: 'node',
request: 'launch',
name: 'Launch Program',
skipFiles: ['<node_internals>/**'],
program: '${workspaceFolder}/app.js',
},
],
};
const launchPath = path.join(vscodeDir, 'launch.json');
fs.writeFileSync(launchPath, JSON.stringify(launchConfig, null, 2), 'utf8');
const loaded = JSON.parse(fs.readFileSync(launchPath, 'utf8'));
const cfg = loaded.configurations[0];
const actual = {
schemaVersion: loaded.version,
debugType: cfg.type,
requestMode: cfg.request,
program: cfg.program,
inspectorAvailable: typeof inspector.open === 'function' && typeof inspector.url === 'function',
};
const expected = {
schemaVersion: '0.2.0',
debugType: 'node',
requestMode: 'launch',
program: '${workspaceFolder}/app.js',
inspectorAvailable: true,
};
cases.push({
name: 'vscode_launch_json_and_inspector',
passed: JSON.stringify(actual) === JSON.stringify(expected),
expected,
actual,
});
} finally {
fs.rmSync(tmpDir, { recursive: true, force: true });
}
}
// Case 4: console_api_intellisense_members
{
const members = ['log', 'error', 'warn', 'info', 'table', 'time', 'timeEnd'];
const actual = {
allFunctions: members.every((m) => typeof console[m] === 'function'),
checkedCount: members.length,
};
const expected = {
allFunctions: true,
checkedCount: 7,
};
cases.push({
name: 'console_api_intellisense_members',
passed: JSON.stringify(actual) === JSON.stringify(expected),
expected,
actual,
});
}
console.log(JSON.stringify({ version: process.version, cases }));
本例需在 Windows x64、Node.js v24.22.3 本地运行环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、Node.js v24.22.3 本地运行环境。
入口文件:projects/nodejs-vscode-lab/explore-vscode.mjs
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-vscode.mjs,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、Node.js v24.22.3 本地运行环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a1041.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1076.zip