The pipe method is one of the most well-known features of streams. It allows us to compose advanced streaming pipelines as a single line of code.
As a part of Node core it can be useful for cases where process uptime isn't important (such as CLI tools).
Unfortunately, however, it lacks a very important feature: error handling.
If one of the streams in a pipeline composed with pipe fails, the pipeline is simply unpiped. It is up to us to detect the error and then afterwards destroy the remaining streams so they do not leak any resources. This can easily lead to memory leaks.
Let's consider the following example:
const http = require('http')
const fs = require('fs')
const server = http.createServer((req, res) => {
fs.createReadStream('big.file').pipe(res)
})
server.listen(8080)
A simple, straightforward...