Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs binary http stream

I need to stream files from a client (nodejs command line) and a server (express nodejs).

This is the client side:

var request = require('request');
var fs = require('fs');

// ...

  var readStream = fs.createReadStream(file.path);
  readStream.on('end', function() {
    that.emit('finished');
  });
  readStream.pipe(request.post(target));

// ...

This is the server side:

var fs = require('fs');
var path = require('path');

// ...

    app.post('/:filename', function(req, res) {
      req.setEncoding('binary');
      var filename = path.basename(req.params.filename);
      filename = path.resolve(destinationDir, filename);
      var dst = fs.createWriteStream(filename);
      req.pipe(dst);
      req.on('end', function() {
        res.send(200);
      });
    });

// ...

All is working, files are saved correctly on the server side... but they are about 50% bigger than the source files. I tried to see difference between the two files with hexdump and the server side file has similar content but with 0xC2 sometimes. I guess this is related to encoding.

like image 212
Alessandro Pezzato Avatar asked Nov 20 '13 17:11

Alessandro Pezzato


1 Answers

Don't call req.setEncoding('binary').

This will convert every single chunk into strings and is mainly intended if you want to read strings from the stream. As you directly pipe the request to a file, you don't need to do it.

like image 119
Paul Mougel Avatar answered Nov 13 '22 19:11

Paul Mougel