I tried to use Busboy in express framework. I want to use it directly without multer just to learn how it works. I stoped on the first example and don't know where the error is. It's quite hard for me to investigate because it just hangs with no error message.
I copied the code from demo from URL: https://www.npmjs.com/package/busboy
And pasted it into my express application as follows:
app.get('/files/new', function(req, res) {
//res.send(req.headers)
res.send('<html><head></head><body>\
<form method="POST" enctype="multipart/form-data" action="/files">\
<input type="text" name="textfield"><br />\
<input type="file" name="filefield"><br />\
<input type="submit">\
</form>\
</body></html>')
})
app.post('/files', function(req, res) {
console.log("files post")
var busboy = new Busboy({ headers: req.headers });
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
file.on('data', function(data) {
console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
});
file.on('end', function() {
console.log('File [' + fieldname + '] Finished');
});
});
busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
console.log('Field [' + fieldname + ']: value: ' + inspect(val));
});
busboy.on('finish', function() {
console.log('Done parsing form!');
res.send("Done")
});
})
I receives following output in the console each time I send the file:
Server started....
files post
The browser is hanged.
Can anyone point me what may cause the problem?
Ok, I found the reason why it hanged. I mistekaly forgot close the connection, below lines:
busboy.on('finish', function() {
res.writeHead(200, { 'Connection': 'close' });
res.end("Done parsing form!");
});