Casey A. Childers Casey A. Childers - 1 year ago 75
HTTP Question

Writing an image to file, received over an HTTP request in Node

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: '',
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;

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download