B
B
Bogdan2019-11-20 21:11:00
Node.js
Bogdan, 2019-11-20 21:11:00

Response readable stream?

Hello. And do not tell me how to return json readable stream in which chunk object through response?

const http = require('http');
const { Readable } = require('stream');

http.createServer((request, response) => {
  const data = [{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }, { e: 5 }];
  const readable = Readable.from(data, { objectMode: true });
  readable.pipe(response);
}).listen(8080);

events.js:187
      throw er; // Unhandled 'error' event
      ^

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string or Buffer. Received type object
    at write_ (_http_outgoing.js:618:11)
    at ServerResponse.write (_http_outgoing.js:586:15)
    at Readable.ondata (_stream_readable.js:726:22)
    at Readable.emit (events.js:210:5)
    at addChunk (_stream_readable.js:308:12)
    at readableAddChunk (_stream_readable.js:289:11)
    at Readable.push (_stream_readable.js:223:10)
    at next (_stream_readable.js:1223:27)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
Emitted 'error' event on Readable instance at:
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Skibin, 2019-11-20
@bogdan_uman

Try like this

const readable = Readable.from(Buffer.from(data), { objectMode: true });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question