蓝色天空.知识 · 实践 · 创造
我的收藏
Node.js 教程目录 38章
Node.js / LEARN BY DOING

Node.js 测验

梳理 Node.js 基础综合自测覆盖的 20 项核心知识点(运行时定位、内置模块、npm 与 package.json、Buffer、全局对象、异步回调与异常处理),并在本地 Node.js v24.22.3 环境中逐项验证对应运行时事实与计分逻辑。

学完这一章,你能够

  • 梳理 Node.js 基础综合自测覆盖的 20 项核心知识点(运行时定位、内置模块、npm 与 package.json、Buffer、全局对象、异步回调与异常处理),并在本地 Node.js v24.22.3 环境中逐项验证对应运行时事实与计分逻辑。

测验定位与 20 题核心考点总览

本章作为 Node.js 基础阶段的综合自测,围绕 20 道高频概念题检验学习者对运行时与常用内置模块的掌握程度,核心考点包括:Node.js 是 JavaScript 运行环境且擅长高并发 I/O 密集型应用;文件系统、Web 服务、路径处理、事件驱动、压缩解压与 URL 解析分别由 `fs`、`http`、`path`、`events`、`zlib`、`url` 模块承担;包管理与第三方依赖安装使用 `npm`、`npm install <包名>` 与项目配置清单 `package.json`。

其余考点进一步覆盖模块与运行时基础:CommonJS 使用 `require()` 导入与 `module.exports` 导出(现代 Node.js 同时原生支持 ESM `import`/`export`);`process` 表示当前进程、`console.log()` 输出控制台日志、`Buffer` 处理二进制字节、`__dirname` 指示当前目录(ESM 中等价为 `import.meta.dirname`);读取 JSON 文件可结合 `fs.readFile()` 与异步回调,`process.on('uncaughtException')` 用于兜底监听未捕获异常。本站未执行来源站外部测验脚本,而是通过独立本地脚本在 Node.js v24.22.3 中直接验证上述内置模块、Buffer、异步 JSON 读写与 20 题确定性计分(满分 `20/20`、错两题 `18/20`),完成后可继续深入后续 Web 应用与 GET/POST 章节。

运行 Node.js 20 项核心测验知识点与本地计分逻辑验证脚本

含完整源码 · 115 行
展开查看收起
javascript
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { builtinModules } from 'node:module';

function makeCase(name, expected, actual) {
  return {
    name,
    passed: JSON.stringify(expected) === JSON.stringify(actual),
    expected,
    actual,
  };
}

// Case 1: verify core built-in modules referenced across Node.js quiz questions
function testCoreModulesAndRuntime() {
  const checked = ['fs', 'http', 'path', 'events', 'zlib', 'url'];
  const expected = {
    isNodeV24: true,
    allQuizModulesBuiltin: true,
  };
  const actual = {
    isNodeV24: process.version.startsWith('v24.'),
    allQuizModulesBuiltin: checked.every((name) => builtinModules.includes(name)),
  };
  return makeCase('core_modules_and_runtime', expected, actual);
}

// Case 2: globals (process, console.log), Buffer binary handling, and directory path
function testGlobalsBufferAndPaths() {
  const buf = Buffer.from('Node', 'utf8');
  const expected = {
    hasProcessObject: true,
    hasConsoleLog: true,
    bufferByteLength: 4,
    bufferIsUint8Array: true,
    dirnameIsAbsolute: true,
  };
  const actual = {
    hasProcessObject: typeof process === 'object' && typeof process.pid === 'number',
    hasConsoleLog: typeof console.log === 'function',
    bufferByteLength: buf.length,
    bufferIsUint8Array: buf instanceof Uint8Array,
    dirnameIsAbsolute: path.isAbsolute(import.meta.dirname),
  };
  return makeCase('globals_buffer_and_paths', expected, actual);
}

// Case 3: async callback JSON file reading and process uncaughtException API presence
async function testAsyncCallbackAndJsonIo() {
  const tmpFile = path.join(os.tmpdir(), `mbsky-quiz-${process.pid}-${Date.now()}.json`);
  fs.writeFileSync(tmpFile, JSON.stringify({ name: 'demo', dependencies: {} }), 'utf8');
  try {
    const parsed = await new Promise((resolve, reject) => {
      fs.readFile(tmpFile, 'utf8', (err, data) => {
        if (err) reject(err);
        else resolve(JSON.parse(data));
      });
    });
    const expected = {
      jsonName: 'demo',
      hasDependenciesObj: true,
      hasProcessOn: true,
    };
    const actual = {
      jsonName: parsed.name,
      hasDependenciesObj: typeof parsed.dependencies === 'object',
      hasProcessOn: typeof process.on === 'function',
    };
    return makeCase('async_callback_and_json_io', expected, actual);
  } finally {
    fs.unlinkSync(tmpFile);
  }
}

// Case 4: deterministic 20-topic quiz scoring helper
function testQuizScoringEngine() {
  const answerKey = [
    'runtime', 'fs', 'npm', 'http', 'path',
    'require', 'events', 'process', 'console.log', 'zlib',
    'binary', '__dirname', 'fs.readFile', 'npm install', 'async',
    'module.exports', 'url', 'uncaughtException', 'package.json', 'io-concurrency',
  ];
  function grade(submissions) {
    let score = 0;
    for (let i = 0; i < answerKey.length; i += 1) {
      if (submissions[i] === answerKey[i]) score += 1;
    }
    return { score, total: answerKey.length };
  }
  const perfect = grade(answerKey);
  const twoWrong = grade(answerKey.map((a, idx) => (idx < 2 ? 'wrong' : a)));

  const expected = {
    questionCount: 20,
    perfect: { score: 20, total: 20 },
    twoWrong: { score: 18, total: 20 },
  };
  const actual = {
    questionCount: answerKey.length,
    perfect,
    twoWrong,
  };
  return makeCase('quiz_scoring_engine', expected, actual);
}

const cases = [
  testCoreModulesAndRuntime(),
  testGlobalsBufferAndPaths(),
  await testAsyncCallbackAndJsonIo(),
  testQuizScoringEngine(),
];

console.log(JSON.stringify({ version: process.version, cases }));
核对 fs/http/path/events/zlib/url 六大内置模块存在性、process/console/Buffer/import.meta.dirname、fs.readFile 异步读取 JSON 及 20 题计分器返回 20/20 与 18/20。

查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载本章实验包 (mbskycom202612a1041.zip) ↓ 查看运行说明 ↗

本章附件与实例包

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

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

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

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

常见错误与边界检查

  • 实际端口、文档资源与应用配置都应和测试条件一致。

技术规范与官方资料

练习与参考解答

查看参考解答

它们分别覆盖了 Node.js 服务端最基础的磁盘 I/O、网络通信、跨平台路径、事件驱动异步模型、流压缩与二进制字节处理能力,构成了绝大多数后端应用和框架的底层依赖。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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