0

I'm just trying to understand the node stream and trying to create a pipe. so reading a file and writing to another writeable stream and again passing to res since that is also the writeable stream.
But it throws the error

Error [ERR_STREAM_CANNOT_PIPE]: Cannot pipe, not readable

Please let me know the reason of the failure and also have a look on below code for intel.

const http = require('http')
const fs = require('fs')

var server = http.createServer((req, res)=>{
    //created the read strem
     let file = fs.createReadStream('./temp_files/file1.txt')
     // pipig this RES, since is RES is the aother writeable stream
     let des = fs.createWriteStream('./temp_files/final.tct')
     file.pipe(des).pipe(res) // ERROR <<<------------------------------

})

server.listen(9090, (req, res)=>{
    console.log("runnig at port 9090")
})
bashIt
  • 1,006
  • 1
  • 10
  • 26
  • May be you need to pass end : false into pipe as shown below or use Appender module they recommend https://stackoverflow.com/a/30916248/3254405 – boateng Jul 10 '18 at 16:03

1 Answers1

2

You cannot chain pipe calls with both writable streams. You need to have a duplex stream in the middle.

So replace file.pipe(des).pipe(res) you can try the following:

file.pipe(des);
file.pipe(res);

Since des is not a transform stream they can work independently and shouldn't make any difference.

Swati Anand
  • 869
  • 6
  • 9