为什么 Github webhook 给我循环 JSON 对象?

Why does Github webhooks give me circular JSON objects?

使用下面的代码,当有人为其提交拉取请求时,我试图从我的 Github 存储库中获取一些基本的拉取请求信息。但我得到的是这个输出:

$ node poc2.js 
sha1=fef5611617bf56a36d165f73d41e527ee2c97d49
res [Circular]
req [Circular]

由于打印了秘密哈希,因此收到了 webhook。我也可以通过 nc -l 8080 而不是 运行 我的 NodeJS 应用程序来验证这一点。将会看到一个大的 json 对象,它来自我在下面的 console.log 中使用的 json 结构。

问题

谁能弄清楚,为什么我从 reqres 得到一个“圆形”json 对象?

如何获得我的值 console.log

const secret = "sdfsfsfsfwerwergdgv";

const http = require('http');
const crypto = require('crypto');

http.createServer(function (req, res) {
    req.on('data', function(chunk) {
        let sig = "sha1=" + crypto.createHmac('sha1', secret).update(chunk.toString()).digest('hex');
        console.log(sig);
        if (req.headers['x-hub-signature'] == sig) {
          console.log("res %j", res);
          console.log("req %j", req);
          if (res.action == 'opened') {

            console.log('PR for: ' + res.repository.html_url);
            console.log('PR for: ' + res.repository.full_name);

            console.log('PR: ' + res.pull_request.html_url);
            console.log('PR title: ' + res.pull_request.title);
            console.log('PR description' + res.pull_request.description);
            console.log('PR by user: ' + res.pull_request.user.login);
          };
        }
    });

    res.end();
}).listen(8080);

req 是循环的,因为它 不是 JSON 对象,它是 Request object,它包含很多内部机制,一些其中循环链接。

实际传入的 JSON 文档在 req.body 或您的特定 JSON body parser puts it 的任何地方,但在本例中您还没有,因此您应该修复它。

Tip: You may want to use Express instead of the core Node http module, it's much more capable.