How can the data obtained by request be saved as Buffer?

  node.js, question
request('http://xxx/ab.jpg', function (error, response, body) {

    if (!error && response.statusCode == 200) {
        var buffer = new Buffer(body);
        }
})

It is not possible to write like this.
How should I write it?

See what you are writing is to tell the picture Buffer to come out. Here is an example:

For example, to request my avatar address:https://sfault-avatar.b0.upaiyun.com/453/230/453230058-573ffdd5eef2b_huge256

var request = require('request');
 var fs = require('fs');
 
 var url = "https://sfault-avatar.b0.upaiyun.com/453/230/453230058-573ffdd5eef2b_huge256";
 var options = {
 url: url,
 encoding: null
 };
 request(options, function(error, response, buffer) {
 if (error) {
 bracket
 
 console.log(typeof(buffer));
 
 fs.writeFile('avatar.png', buffer, function(err) {
 if (err) {
 bracket
 });
 });

The main problem you are currently facing is that you are returning a String of type String, using the default encoding format UTF-8. InrequestTheOfficial website introductionIt is pointed out thatrequest(options, callback)Part ofoptionsSetencoding

encoding– Encoding to be used on setEncoding of response data.If null, the body is returned as a Buffer.Anything else (including the default value of undefined) will be passed as the encoding parameter to toString() (meaning this is effectively utf8 by default). (Note: if you expect binary data, you should set encoding: null.)

Therefore, you willencodingSet tonullAfter that,requestWill return directlyBufferOf type, that is:

var options = {
 url: url,
 encoding: null
 };

I hope the above can help you.