Search code examples
javascriptnode.jsexpressbusboy

busboy won't receive uploaded files


My form is simple. It uses ng-flow to handle file uploads:

<form class="form-horizontal" enctype="multipart/form-data">
    <div flow-init="{target: '/test', testChunks: false, query: {'_csrf': '{{csrf}}', 'somestring': 'teststring'} }"
         flow-files-submitted="data.flow.upload()"
         flow-name="data.flow">

         <input type="file" flow-btn/>
    </div>
</form>

Once an image is selected, ng-flow will do a POST to the target route. It seems like the image was sent since the Request Payload has a bunch of things like:

1048576
------WebKitFormBoundaryw2YAG9m602ICPd0Q
Content-Disposition: form-data; name="flowCurrentChunkSize"

The image isn't very big (~1MB)

On the nodejs side (with express):

var busboy = require('connect-busboy')({
    limits: {
        fileSize: 10 * 1024 * 1024
    }
});
router.post('/test', busboy, function(req, res) {
    console.log('test called');
    console.log(req.busboy);
    if (req.busboy) {
        req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
            console.log("this is fieldname: " + fieldname);
        });
        req.busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
            console.log('Field [' + fieldname + ']: value: ' + inspect(val));
        });
    }

    res.json();
});

req.busboy returns an object full of things, but req.busboy.on('file'... and req.busboy.on('field'...) never trigger.

Why isn't busboy seeing my strings and images?


Solution

  • You need to pipe the request to busboy so it can parse the form:

    req.pipe(req.busboy);