Skip to content

Stream response #157

Answered by franciscop
lantica asked this question in Q&A
Aug 29, 2024 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

Thanks for asking! Sure, there are multiple ways, please see this comment:

// Streaming a file:
const { file } = require('server/reply');
const mid = () => file("./path/myfile.mp3");

For arbitrary data:

// Untested, but you might/should be able to stream like this:
const { pipeline } = require('node:stream/promises');
const mid = async (ctx) => {
  ctx.res.setHeader('Content-Type', 'audio/mp3');
  await pipeline(nodeReadStream, ctx.res);
};
// If the above didn't work, this is verified to work:
const mid = (ctx) => {
  ctx.res.setHeader('Content-Type', 'audio/mp3');
  return new Promise((resolve) => {
    nodeReadStream.pipe(ctx.res).on('end', resolve);
  });
};

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@lantica
Comment options

@franciscop
Comment options

Answer selected by lantica
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants