Nodejs binary http stream

binaryfiles, express, node.js, stream

Solution

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.

Problem

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.

Original source