NodeJS 请求:如何获取字符串形式的响应?

NodeJS Request: How to get the response as a string?

我有点迷路了。是否可以在 .on(response) 中将响应主体作为字符串获取(见下文)?还是我疯了?我知道我可以 .pipeWriteStream 的响应,但我需要将响应正文和响应 statusCode 放在同一位置才能继续我的代码。谢谢!

var request = require("request");
request(url)
.on('response', function(response) {
    console.log(response.statusCode); // 200
    console.log(/* I need to get response body string here */); // <--- can I have the response body as a string here?
})
.on("error", function(err){
    console.log("Problem reaching URL: ", err);
});

我想发表评论,但我不能。

如同在 npm 页面中一样(考虑到您不需要管道),您可以将正文与响应一起放在::

var request = require("request");

request(url, function (error, response, body) {
  console.log('error:', error); 
  console.log('statusCode:', response && response.statusCode); 
  console.log('body:', body); 
});