【问题标题】:Weird output from console.log(res) NodeJSconsole.log(res) NodeJS 的奇怪输出
【发布时间】:2018-12-02 11:32:31
【问题描述】:

在对来自 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);
});
...

【问题讨论】:

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


【解决方案1】:

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

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

输出:

示例 { foo: 'bar' }

您告诉它记录 ServerResponse 对象,它确实如此,创建类似于上面的输出,只是具有更多属性。

【讨论】:

  • 不要介意我缺乏知识,但你能解释一下E类{};之间的区别吗?让 a = 新 E;让 a = {};因为它们都有不同的输出
  • @VipulBhardwaj 一个a 是一个instanceof E(它继承自E.prototype)而另一个不是。
  • @VipulBhardwaj - 添加到上面的 Bergi 评论:输出包括与对象关联的构造函数的名称。在new E 中,构造函数是E。在{} 中,它是Object
  • Umm ..我在这里真的很困惑,谢谢大家的帮助。我将尝试阅读更多关于差异的信息并尝试了解整个情况。
猜你喜欢
  • 1970-01-01
  • 2011-09-24
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 2021-12-03
  • 2015-02-18
  • 2011-06-14
  • 2013-06-04
相关资源
最近更新 更多