Sunday, May 29, 2016

I haven't seen an example in node.js where an http response stream is piped to a downloadable file. So here it goes :


var path = require('path');

var mime = require('mime');



app.get('/download', function(req, res){



  var file = __dirname + '/upload-folder/dramaticpenguin.MOV';



  var filename = path.basename(file);

  var mimetype = mime.lookup(file);



  res.setHeader('Content-disposition', 'attachment; filename=' + filename);

  res.setHeader('Content-type', mimetype);



 var options = {

    host: 'sourceserver.com',

    port: 443,

    path: '/resource/',

    method: 'get',

    headers: {

    'Content-Type': 'application/octet-stream',

    'Authorization': 'Basic some_token',

    }

  };

  var httpreq = https.request(options, function (response) {

   // a response is a readable stream

   response.pipe(res);

   });

   httpreq.end();

});

#codingexercise
 A linked list has values sorted by absolute values. Sort the linked list by actual values
 void SortByActual(ref Node root)
{
if (root == null) return;
Node prev = root;
Node curr = prev.next;
while ( curr != null)
{
 if (curr.data < prev.data)
{
prev.next = curr.next;
Push(ref root, curr);
curr = prev;
}
else{
prev = curr;
}
curr = curr.next;
}

}

No comments:

Post a Comment