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



使用下面的代码,我试图从我的Github repo获得一些基本的拉请求信息,当有人提交它的拉请求。但是我得到了这样的输出:

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

因为秘密哈希被打印出来,所以webhook被接收了。我也可以通过nc -l 8080来验证这一点,而不是运行我的NodeJS应用程序。会看到一个大的json对象,这是从我得到的json结构,我在console.log's下面使用。

<标题>

有谁能弄明白,为什么我会收到"通知"吗?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对象,其中包括许多内部机制,其中一些是循环链接。

实际传入的JSON文档是在req.body或任何你的特定JSON体解析器放它,虽然在这个例子中你还没有一个,所以这是你应该修复的东西。

提示:您可能希望使用Express而不是核心Nodehttp模块,它功能更强大。

最新更新