如何解决有没有办法记录节点获取请求?
所以我尝试使用 node-fetch 在 node.js 中使用一些 API,我想记录发送到服务器的最终请求,但我找不到任何方式怎么做。你能帮我吗?代码如下:
const rightallocation = [];
for (let i=0; i < 5; i++) {
rightallocation.push({number: 1});
}
解决方法
node-fetch
库本身似乎没有内置任何调试或日志记录功能(通过在 github 上仔细阅读 the source)。但是,它构建在 http
库之上,该库确实具有一些日志记录/调试功能。
它不会显示确切的传出请求,但会显示在请求构建到最终 http 请求之前收集的所有数据。例如,如果您在运行程序之前在您的环境中设置了 NODE_DEBUG=http
,那么您可以获得如下输出:
HTTP 20624: call onSocket 0 0
HTTP 20624: createConnection google.com:80: {
protocol: 'http:',slashes: true,auth: null,host: 'google.com',port: 80,hostname: 'google.com',hash: null,search: null,query: null,pathname: '/',path: null,href: 'http://google.com/',method: 'GET',headers: [Object: null prototype] {
'Some-Header': [ 'someValue' ],Accept: [ '*/*' ],'User-Agent': [ 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)' ],'Accept-Encoding': [ 'gzip,deflate' ],Connection: [ 'close' ]
},agent: undefined,servername: 'google.com',_agentKey: 'google.com:80:'
}
HTTP 20624: sockets google.com:80: 1 1
HTTP 20624: outgoing message end.
(node:20624) Warning: Setting the NODE_DEBUG environment variable to 'http' can expose sensitive data (such as passwords,tokens and authentication headers) in the resulting log.
(Use `node --trace-warnings ...` to show where the warning was created)
HTTP 20624: requestTimeout timer moved to req
HTTP 20624: AGENT incoming response!
HTTP 20624: call onSocket 0 0
HTTP 20624: createConnection www.google.com:80: {
protocol: 'http:',host: 'www.google.com',hostname: 'www.google.com',href: 'http://www.google.com/',servername: 'www.google.com',_agentKey: 'www.google.com:80:'
}
HTTP 20624: sockets www.google.com:80: 1 2
HTTP 20624: outgoing message end.
HTTP 20624: CLIENT socket onClose
HTTP 20624: removeSocket google.com:80: writable: false
HTTP 20624: HTTP socket close
HTTP 20624: requestTimeout timer moved to req
HTTP 20624: AGENT incoming response!
done
HTTP 20624: AGENT socket.destroySoon()
HTTP 20624: CLIENT socket onClose
HTTP 20624: removeSocket www.google.com:80: writable: false
HTTP 20624: HTTP socket close
而且,如果您设置:
NODE_DEBUG=http,net,stream
您将获得更多信息。尽管来自 http 模块的数据向您展示了将被组装到该请求中的内容,但我仍然没有看到通过此调试获得为 http 请求发送的确切数据的任何方法。您可能必须使用代理或网络记录器才能查看发送到服务器的确切流。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。