6
I have a question about how the .pipe()
. I saw videos and read the documentation, but I’m not sure how it works.
From what I understand, he basically takes the information from a readableStream
and pass it on to a writeableStream
, but I’m not sure I know how to use it the right way. To clarify what I’m saying I’ll put a code of mine that went wrong:
const fs = require('fs')
const http = require('http')
const port = 8000
const file = './file.txt'
const readStream = fs.createReadStream(file)
const server = http.createServer((req, res) => {
res.on('pipe', fileContent => console.log(fileContent.toString()))
readStream.pipe(res)
})
server.listen(port, () => console.log('Server is Runing...'))
My goal in this part was to create an http server and pass the contents inside the file through the console as a response (Obs: in the file only has any text of a line). My server runs, but nothing appears on the console. I wanted to understand what might be going wrong because I do not know how to receive the contents of the file I sent.