来自console.log(res)NodeJS的奇怪输出

时间:2018-12-02 11:32:32

标签: javascript node.js server response console.log

在createServer的响应对象上使用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);
});
...

enter image description here

1 个答案:

答案 0 :(得分:0)

这并不特殊。 res是一个对象,而不是字符串。这就是Node.js的console.log显示对象的方式。例如:

class Example {
    constructor() {
        this.foo = "bar";
    }
}
console.log(new Example);

输出:

Example { foo: 'bar' }

您要告诉它记录一个ServerResponse对象,这样做,并创建与上述类似的输出,只是具有更多的属性。