Processing file updloads on the server.
Streams in NodeJS are incredibly powerful. I found out myself for a site I’m working on where users are allowed to upload images from their mobile devices. Images are typically sent from a camera photo but they can also be selected from a local gallery or clould storage. Desktop users can use the ‘file-open’ dialog.
The code below is a request handler written in coffee-script that handles a mult-part http POST (containing a file) from an Express route. Steams are modern, async and event-driven. Instead of writing temp files to the file-system, we stream the file into a mongoDB GridStore. Streams are great for uploads, not only because a file doesn’t touch a disk file-system but they are processed in a non-blocking, asyncronous fashion. A stream doesn’t have to process a large file in one buffer either which allows the server to use memory more efficiently. With busboy the file’s stream pipes it’s way straight into mongoDB via gridfs-steam).
For extra-bonus points: What does the 201/created JSON response look like?