1

I have this code so far, but can not get the Buffer bianary.

var http = require('http');
var myServer = http.createServer(function(request, response)
{
    var data = '';

    request.on('data', function (chunk){
        data += chunk;
    });

    request.on('end',function(){

        if(request.headers['content-type'] == 'image/jpg') {

            var binary = Buffer.concat(data);

            //some file handling would come here if binary would be OK

            response.write(binary.size)
            response.writeHead(201)
            response.end()
        }

But get this error: throw new TypeError('Usage: Buffer.concat(list, [length])');

1 Answer 1

1

You're doing three bad things:

  1. Using the Buffer API wrong - hence the error message.
  2. Concatenating binary data as strings
  3. Buffering data in memory

Mukesh has dealt with #1, so I'll cover the deeper problems.

First, you're receiving binary Buffer chunks and converting them to strings with the default (utf8) encoding, then concatenating them. This will corrupt your data. As well as there existing byte sequences that aren't valid utf8, if a valid sequence is cut in half by a chunk you'll lose that data too.

Instead, you should keep the data always as binary data. Maintain an array of Buffer to which you push each chunk, then concatenate them all at the end.

This leads to the problem #3. You are buffering the whole upload into memory then writing it to a file, instead of streaming it directly to a (temporary) file. This puts a lot of load on your application, both using up memory and using up time allocating it all. You should just pipe the request to a file output stream, then inspect it on disk.

If you are only accepting very small files you may get away with keeping them in memory, but you need to protect yourself from clients sending too much data (and indeed lying about how much they're going to send).

Sign up to request clarification or add additional context in comments.

1 Comment

@MukeshSharma but not complete without yours, can you undelete it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.