Busboy Not Firing Finish Event
I am trying to pass a request object from my routes to a controller which processes the uploads, here is the route - app.post('/upload/notes',auth.requiresApiLogin,function(req,re
Solution 1:
You need to consume the file
stream somehow. For testing purposes you can ignore the data by adding file.resume();
inside the file event handler.
Solution 2:
This answer might help someone who wants to consume the file...
Make sure to pass control back to express by calling next() in the finish method after consuming the file e.g.
app.use(function(req: Request, res: ServerResponse, next:any) {
let bb = newbusboy({ headers: req.headers });
letfileData: any = null;
constfieldArray: any = [];
letdataLength: number;
constextractFields = (name: string, val: any, data: any) => {
if (Array.isArray(data[name])) {
data[name].push(val);
} elseif (data[name]) {
data[name] = [data[name], val];
} else {
data[name] = val;
}
};
bb.on('field', function(fieldname: string, val:any, fieldnameTruncated: boolean, valTruncated: boolean, encoding: string, mimetype: string) {
// extract fields to add to req body for later processingextractFields(fieldname, val, fieldArray);
console.log('Field [' + fieldname + ']: value: ' + val);
});
bb.on('file', function(fieldname: string, file: NodeJS.ReadableStream , filename: string, encoding: string, mimetype: string) {
file.on('data', function(data) {
console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
// Process buffer to save complete file to add to the req for access laterif (fileData === null) {
fileData = data;
} else {
fileData = Buffer.concat([fileData, data]);
}
});
file.on('end', function() {
console.log('File [' + fieldname + '] Finished');
});
});
bb.on('finish', function() {
req.body = fieldArray
req.body.files = {file: fileData};
console.log('Done parsing form!');
next(); // <- Allows processing to continue and avoids request hanging
});
bb.on('close', () => {
console.log('Event closed')
});
// what does this do? Send the readable request stream to busboy which is a writeable stream. The above code dictates to the writeable// how to respond when certain events are fired during the pipe.
req.pipe(bb);
});
Post a Comment for "Busboy Not Firing Finish Event"