|
| 1 | +### Why [bus-boy](https://github.com/mscdex/busboy) is needed |
| 2 | + |
| 3 | + |
| 4 | +#### A node.js module for parsing incoming HTML form data. Its used to upload files. Busboy is a Writable stream and its an alternative for multer. A writable stream is an abstraction for a destination to which data can be written. An example of that is the ``fs.createWriteStream`` method. |
| 5 | + |
| 6 | +On busboy 'file' event you get parameter named 'file' and this is a stream so you can pipe it. |
| 7 | + |
| 8 | + |
| 9 | +```js |
| 10 | +busboy.on('file', function(fieldname, file, filename, encoding, mimetype) => { |
| 11 | + file.pipe(streamToSQS) |
| 12 | +}) |
| 13 | +``` |
| 14 | +[An example of file upload with busboy and express](https://gist.github.com/shobhitg/5b367f01b6daf46a0287) |
| 15 | + |
| 16 | +```js |
| 17 | +// accept POST request on the homepage |
| 18 | +app.post('/', function (req, res) { |
| 19 | + var busboy = new Busboy({ headers: req.headers }); |
| 20 | + busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { |
| 21 | + var saveTo = path.join('.', filename); |
| 22 | + console.log('Uploading: ' + saveTo); |
| 23 | + file.pipe(fs.createWriteStream(saveTo)); |
| 24 | + }); |
| 25 | + busboy.on('finish', function() { |
| 26 | + console.log('Upload complete'); |
| 27 | + res.writeHead(200, { 'Connection': 'close' }); |
| 28 | + res.end("That's all folks!"); |
| 29 | + }); |
| 30 | + return req.pipe(busboy); |
| 31 | + |
| 32 | +}); |
| 33 | +``` |
| 34 | + |
| 35 | +The function ``fs.createWriteStream()`` creates a writable stream in a very simple manner. After a call to fs.createWriteStream with the filepath, you have a writeable stream to work with. |
| 36 | + |
| 37 | +## Comparison with multer |
| 38 | + |
| 39 | + |
| 40 | +Some developers opine that Multer is easier because it abstracts away some of the details of Busboy. |
| 41 | + |
| 42 | +### Difference between busboy and connect-busboy |
| 43 | + |
| 44 | +[https://stackoverflow.com/questions/39439922/difference-between-busboy-and-connect-busboy](https://stackoverflow.com/questions/39439922/difference-between-busboy-and-connect-busboy) |
| 45 | +Connect is a middleware layer for building servers in Node.js. It was originally the basis for the Express web framework. |
| 46 | + |
| 47 | +What middleware here really means is essentially an array of functions that conform to an interface which get called on each request in the order they are defined. |
| 48 | + |
| 49 | +connect-busboy wraps the busboy library into a connect compatible middleware. You can see in the source it really just returns a function. |
| 50 | + |
| 51 | +If you're using express you might want to take a look at express-busboy which uses connect-busboy under the hood and has recent updates. |
| 52 | + |
| 53 | +### What exactly are streams?* |
| 54 | + |
| 55 | +Streams are collections of data — just like arrays or strings. The difference is that streams might not be available all at once, and they don’t have to fit in memory. This makes streams really powerful when working with large amounts of data, or data that’s coming from an external source one chunk at a time. |
| 56 | + |
| 57 | +However, streams are not only about working with big data. They also give us the power of composability in our code. Just like we can compose powerful linux commands by piping other smaller Linux commands, we can do exactly the same in Node with streams. |
| 58 | + |
| 59 | +#### Here’s the magic line about pipe() and stream that you need to remember: |
| 60 | + |
| 61 | +``readableSrc.pipe(writableDest)`` |
| 62 | + |
| 63 | +In this simple line, we’re piping the output of a readable stream — the source of data, as the input of a writable stream — the destination. |
0 commit comments