in Education by
I'm certain I'm missing something obvious, but the gist of the problem is I'm receiving a PNG from a Mapbox call with the intent of writing it to the file system and serving it to the client. I've successfully relayed the call, received a response of raw data and written a file. The problem is that my file ends up truncated no matter what path I take, and I've exhausted the answers I've found skirting the subject. I've dumped the raw response to the log, and it's robust, but any file I make tends to be about a chunk's worth of unreadable data. Here's the code I've got at present for the file making. I tried this buffer move as a last ditch after several failed and comparably fruitless iterations. Any help would be greatly appreciated. module.exports = function(req, res, cb) { var cartography = function() { return https.get({ hostname: 'api.mapbox.com', path: '/v4/mapbox.wheatpaste/' + req.body[0] + ',' + req.body[1] + ',6/750x350.png?access_token=' + process.env.MAPBOX_API }, function(res) { var body = ''; res.on('data', function(chunk) { body += chunk; }); res.on('end', function() { var mapPath = 'map' + req.body[0] + req.body[1] + '.png'; var map = new Buffer(body, 'base64'); fs.writeFile(__dirname + '/client/images/maps/' + mapPath, map, 'base64', function(err) { if (err) throw err; cb(mapPath); }) }) }); }; cartography(); }; JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
It is possible to rewrite your code in more compact subroutine: const fs = require('fs'); const https = require('https'); https.get(url, (response)=> { //request itself if(response) { let imageName = 'image.png'; // for this purpose I usually use crypto response.pipe( //pipe response to a write stream (file) fs.createWriteStream( //create write stream './public/' + imageName //create a file with name image.png ) ); return imageName; //if public folder is set as default in app.js } else { return false; } }) You could get original name and extension from url, but it safer to generate a new name with crypto and get file extension like i said from url or with read-chunk and file-type modules.

Related questions

0 votes
    Unlike writing out a table or CSV file, dump() and dput() preserve the ______ so that another user ... Operations of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Which of the following are the features of an HTTP request? (a) URL being requested (b) Optional ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    @RequestMapping annotation is used to map a HTTP request method (GET or POST) to a specific class or method in the ... which will handle the respective request. A. True B. False...
asked Nov 8, 2022 in Education by JackTerrance
0 votes
    Is it possible to make a timeout of 3 seconds in a post request ? How ? My code for the moment ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    I am a very novice C# person so please dont be too hard on me Im trying to make a post request ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    How can I transform QraphQl query to HTTP get request for example http://localhost:4000/? query GetClient { ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    When dealing with mobile clients it is very common to have multisecond delays during the transmission of HTTP ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    What I'm trying to do here is get the headers of a given URL so I can determine the MIME type. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I have the following code (just as a test) and I want to create an HTTP proxy using EventMachine. The code below ... , data, b].inspect end conn.on_data do |data| @buffer...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Can you help me out with this program? I am unable to run this code. varx http = require('http'); exports.handler ... + eventa.url); } Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    Which is the element that has a src property to initiate HTTP GET request? (a) img (b) iframe (c ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    How will the HTTP GET request be sent from the browser? (a) Remote server (b) Local server (c) ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    What are the core components of a HTTP Request in RESTful?...
asked Nov 7, 2020 in Technology by JackTerrance
0 votes
    Which of these exception is thrown in cases when the file specified for writing is not found? (a) IOException (b ... & Applets of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    I am trying to write my below pinging script results into the Text file, but I am getting an error message. ... object is not iterable Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
...