蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线验证 Node.js 项目清单、app.js 回环 HTTP 服务、launch.json 调试配置与 console 成员

JAVASCRIPTWindows x64、Node.js v24.22.3 本地运行环境。explore-vscode.mjs
explore-vscode.mjs152 行
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

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-vscode.mjs,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、Node.js v24.22.3 本地运行环境。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Node.js

课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a1041.zip

下载课程关联工程包:Node.js

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1076.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习