7

I was wondering if it is possible to get Busboy to parse the fields and files separately. (I have removed bodyParser as you can fill the hard drive with temp files very easily.)

For instance - middleware which parsers post fields (used on all POST requests)

if (req.method === 'POST') {
    var form = new busboy({ headers: req.headers, limit: { files: 0 } });

    form.on('field', function (fieldname, val, valTruncated, keyTruncated) {
        req.params.body[fieldname] = val;
    });
    form.on('finish', function () {
        next();
    });
    return req.pipe(form);

} else { next(); }

Then on upload pages use the following, which uses Busboy to get the posted files.

app.post('/fm/file/upload/:folder', function (req, res) {
    var isFrame = helpers.toBool(req.param('frame'), false),
        uploadService = fmUploadService.create(),
        userId = res.locals.info.User.Id,
        folder = helpers.toInt(req.param('folder', 0));

uploadService.processUploads(userId, folder, req, res, function (uploadError, allowedAccess, files) {
        if (uploadError) {
            if (req.xhr) {
                res.status(500);
                res.json(uploadError);
            } else {
                res.render('filemanager/file_upload.jade', { actionUrl: '/fm/file/upload/' + folder, tempData: files, isFrame: isFrame, err: uploadError });
            }
            return;
        }
        else if (req.xhr) {
            res.status(200);
            res.json(files);
            return;
        }
        res.render('filemanager/file_upload.jade', { actionUrl: '/fm/file/upload/' + folder, tempData: files, isFrame: isFrame, err: null });
    });
});

Currently files will always be 0 as Busboy has already run within the middleware.

Mizzrym
  • 149
  • 4
  • 9
  • 3
    Did you ever find a solution for this? I have the same use case, want to parse files in my model, but catch the fields as middleware to populate the req.body before hitting the routes. – Scott May 14 '14 at 01:17
  • 2
    I am looking for a solution for this. Any updates? – Marcel Batista Oct 14 '14 at 06:27
  • I'm not sure if I understand the question correctly, but in Express 4, Multer handles the file uploads separately. – sluijs Sep 11 '15 at 12:15

1 Answers1

0

Instead of

form.on('finish', function () {
    next();
});`

try it with

form.on('end', function () {
    next();
});`   
Reeno
  • 5,720
  • 11
  • 37
  • 50
Ketha Kavya
  • 558
  • 6
  • 18