Tuesday, 2 May 2017

Node only writes half of binary data

This is my simple express service:

let express = require('express'),
    fs      = require('fs'),
    path    = require('path');

var app = express();
app.set("json spaces", 2);
app.use(express.static(__dirname + "/static"));

app.use(function(req, res, next) {
  var data = new Buffer('');
  req.on('data', function(chunk) {
      data = Buffer.concat([data, chunk]);
  });
  req.on('end', function() {
    req.rawBody = data;
    next();
  });
});

app.post("/upload/:filename", function(req, res) {
    let path = "/tmp/" + req.params.filename;
    req.pipe(fs.createWriteStream(path));
    fs.writeFile(path, req.rawBody, "binary", function(error) {
        res.send({
            message: "sadfasdf"
        });
    });
});

app.listen(3001, function() {
    console.log("Started!");
});

I'm doing this to upload the binary data:

curl -H "Content-Type: application/octet-stream" -X POST http://localhost:3001/upload/test.tiff -d @/home/vagrant/test.tiff

For a file that is about 1100kb, it will only actually write about 622kb. Can someone tell me what is going wrong?



via Mike Thomsen

No comments:

Post a Comment