Uploading and Downloading Files: Streaming in Node.js

By June 24, 2019 Uncategorized

While the buffer APIs are easier to use to upload and download files, the streaming APIs are a great way to better manage memory and concurrency. In this post, you’ll learn how to stream files between clients, Node.js, and Oracle Database.



The streaming APIs in Node.js are designed to leverage and simplify its evented nature. There are four different stream classes: Readable, Writeable, Transform, and Duplex. Readable streams (which includes Transform and Duplex) have a pipe method that can be thought of like the pipe command in Linux, where the standard output of one command can be piped into the standard input of another command. Here’s a command line example that takes the output from the ls (list) command and pipes it through the grep (search) command to show files that have the word “oracle” in the name:

With Node.js, the syntax is a bit different. Instead of the pipe command, the pipe method of a readable stream is invoked and passed a writeable stream:

The pipe method returns the destination stream, which allows multiple calls to be chained together as a pipeline. Here’s an example:

The pipe method includes built-in backpressure support. If the consumer is unable to consume data as fast as the producer can produce it, then the producer will be paused until the consumer catches up.

Error handling can be a little tricky with streams. By default, errors are not forwarded upstream with pipe because it would be difficult to determine where the error occurred. Errors can be forwarded manually or the pipeline method of the stream module can be used to automate error forwarding and cleanup.

Uploading files

Controller logic

As mentioned in the post on buffering, the request object passed to the request listener is a readable stream. To validate the file size, I create a FileValidator class based on the Transform class and then pipe the request stream into a new instance of the FileValidator class. The FileValidator instance is then passed to the database API for processing.


  • Lines 1-31: The Transform class is required in from the stream module and used to create a new FileValidator class. The FileValidator class tracks the number of bytes streaming through it and raises an error if the total exceeds its maxFileSize property.
  • Lines 36-48: A number of variables are declared, including an instance of the FileValidator class. The readable request stream is then piped into the FileValidator instance.
  • Lines 51-53: A listener is added to the aborted event of the request stream. If the event is triggered, then no further processing is needed by the database API, so an error is manually triggered on the FileValidator instance.
  • Lines 55-71: The FileValidator instance is passed to the database API’s create method. If successful, the id of the file is returned to the client, otherwise an error is returned.
Database logic

Once the controller logic has passed along the readable stream and related metadata, the database logic can begin to stream in the file contents. To start, an empty blob is inserted into the database and a pointer to it is returned as an out bind. The pointer is an instance of the Lob class of node-oracledb.

The Lob class is implemented as a Duplex stream, so it supports both reading and writing depending on the context. The readable stream is piped into the Lob instance, and when the file finishes streaming in the transaction is committed.


  • Lines 1-14: A SQL insert statement is declared. Within it, an empty blob is inserted into the blob_data column and returned out via the returning clause and the bind variable named :content_buffer.
  • Lines 18-41: The streaming implementation requires a database connection to stay open while the data is being streamed. A connection is obtained from the connection pool and the SQL statement is executed. The Lob instance is obtained from the out binds on the result object and then contentStream is piped into it.
  • Lines 43-46: Errors triggered on the contentStream instance are triggered on the Lob instance create a single error handling code path.
  • Lines 48-56: If an error occurs on the Lob instance, the connection to the database is closed – this will rollback the transaction automatically.
  • Lines 58-72: When the finish event is triggered on the Lob instance, the transaction is commited and the id of the row is returned to the controller logic.
Downloading files
Controller logic

When downloading a file from the database, the controller logic will be passed an instance of the Lob class from the database API. In this case, the Lob instance is a readable stream, so its output can be piped into the response object which will stream the data to the client.


  • Lines 3-21: The id of the file to be downloaded is parsed from the request parameters and passed along to the get method of the database API to fetch the row with the readable stream and related metadata. A listener is added to the aborted event before requesting row because that’s an async operation and the event could happen while waiting for the row to be returned.
  • Lines 31-40: If the database API successfully returns a row, then the HTTP response status is set to 200 OK. Next, HTTP headers (including metadata related to the file) are added to the response. Smart clients, such as browsers, know how to use such headers when downloading files. Finally, the file contents are streamed to the client by piping the Lob stream to the response object.
Database logic

When it comes to fetching the file content back out of the database, the default type for a BLOB column is the Lob/readable stream, so no code needs to be added for that. The trick is to wait until the streaming is done before closing the connection to the database.


  • Lines 1-7: A SQL statement to fetch the file data is declared.
  • Lines 10-36: A variable is declared for the bind definitions (no execute options are needed as the default return type of Lob is what is needed). The query is executed and an event listener is added to the close event of the Lob to close the to the database connection. An additional listener is added to the error event to ensure connections are closed when errors occur. Finally, the fetched row is returned to the controller logic.

As you can see, implementing file upload and download capabilities using the streaming APIs is a little more complex than with the buffer APIs. Most of the complexity is based around the separation of concerns (controller vs database logic) and figuring out when connections should be closed and how errors should be handled. However, if you want to work with very large files (over 1 GB) and optimize memory utilization within Node.js, then these APIs are the key.

Leave a Reply