console.log(res) NodeJS 的奇怪输出
Weird output from console.log(res) NodeJS
在对来自 createServer 的响应 Obj 使用 console.log 时,输出会在打印实际对象数据之前打印 ServerResponse。我想知道res有什么特别之处,会造成这种效果???
...
http.createServer((req, res) => {
// Parsing and Logging endpoint on console.
const URL = url.parse(req.url, true).pathname;
console.log(res);
});
...
没什么特别的。 res
是对象,不是字符串。这就是 Node.js 的 console.log
显示对象的方式。例如:
class Example {
constructor() {
this.foo = "bar";
}
}
console.log(new Example);
输出:
Example { foo: 'bar' }
你告诉它记录一个 ServerResponse
对象,它确实如此,创建类似于上面的输出,只是有更多的属性。
在对来自 createServer 的响应 Obj 使用 console.log 时,输出会在打印实际对象数据之前打印 ServerResponse。我想知道res有什么特别之处,会造成这种效果???
...
http.createServer((req, res) => {
// Parsing and Logging endpoint on console.
const URL = url.parse(req.url, true).pathname;
console.log(res);
});
...
没什么特别的。 res
是对象,不是字符串。这就是 Node.js 的 console.log
显示对象的方式。例如:
class Example {
constructor() {
this.foo = "bar";
}
}
console.log(new Example);
输出:
Example { foo: 'bar' }
你告诉它记录一个 ServerResponse
对象,它确实如此,创建类似于上面的输出,只是有更多的属性。