控制台(Console)


    模块提供了一个简单的调试控制台,与 Web 浏览器提供的 JavaScript 控制台机制类似。

    • 一个 Console 类,包含类似于 console.log()console.error()console.warn() 这些方法,可以用于写入任何的 Node.js 流。

    使用全局 的示例:

    1. const out = getStreamSomehow();
    2. const myConsole = new console.Console(out, err);
    3. myConsole.log('hello world');
    4. // 在 stdout 中打印: hello world
    5. myConsole.log('hello %s', 'world');
    6. myConsole.error(new Error('Whoops, something bad happened'));
    7. // 在 stderr 中打印: [Error: Whoops, something bad happened]
    8. const name = 'Will Robinson';
    9. myConsole.warn(`Danger ${name}! Danger!`);

    虽然 Console 类的 API 是根据浏览器的 console 对象设计的,但 Node.js 中的 并没有完全复制浏览器中的功能。