Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
Issue with loading chunks into the client

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()) })
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you should add a pipe function not a regular write to the response

  readStream.on('open', function () {
    // This just pipes the read stream to the response object (which goes to the client)
    readStream.pipe(res);
  });

  // This catches any errors that happen while creating the readable stream (usually invalid names)
  readStream.on('error', function(err) {
    res.status(500).end(err);
  });

for more information: https://nodejs.org/en/knowledge/advanced/streams/how-to-use-fs-create-read-stream/

I hope it help you :)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda