I'm trying to create a readableStream through which I would send chunks of data ( html in this case ). The problem is that even if I manage to get a chunk of data the res.write()
method does nothing and just returns false if I log it.
I wonder why is that?
import { createServer } from 'http'
import { createReadStream } from 'fs'
const server = createServer((req, res) => {
if (req.method === 'GET') {
if (req.url === '/') {
res.writeHead(200, { 'Content-Type': 'text/html' })
let readStream = createReadStream('./src/public/index.html', { encoding: 'utf8' })
readStream.on('data', (chunk) => {
console.log(chunk)
// html code
res.write(chunk)
// does nothing
})
res.end()
}
}
})
const PORT = 3000 || 8080
server.listen(PORT, () => { console.log(server.address()) })