控制台(Console)
模块提供了一个简单的调试控制台,与 Web 浏览器提供的 JavaScript 控制台机制类似。
一个
Console
类,包含类似于console.log()
、console.error()
和console.warn()
这些方法,可以用于写入任何的 Node.js 流。
使用全局 的示例:
const out = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// 在 stdout 中打印: hello world
myConsole.log('hello %s', 'world');
myConsole.error(new Error('Whoops, something bad happened'));
// 在 stderr 中打印: [Error: Whoops, something bad happened]
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
虽然 Console
类的 API 是根据浏览器的 console
对象设计的,但 Node.js 中的 并没有完全复制浏览器中的功能。