question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't pipe stream to ctx.res

See original GitHub issue

I’m trying to pipe streams to my response but it seems didn’t work

Simple test case:

const Koa = require('koa');
const app = new Koa();
const Router = require('koa-better-router');
const router = Router().loadMethods();
const request = require('request');

app.use(async (ctx, next) => {
    const start = new Date();
    await next();
    const ms = new Date() - start;
    console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});

router.get('/', async (ctx) => {
    ctx.req.pipe(request(`https://www.google.com/?q=${ctx.query.q}`)).pipe(ctx.res);
});

app.use(router.middleware());

app.listen(3000, () => {
    console.log('listening on port 3000');
});

An error occurred:

$ node index.js 
listening on port 3000
GET / - 35ms
internal/streams/legacy.js:59
      throw er; // Unhandled stream error in pipe.
      ^

Error: write after end
    at ServerResponse.write (_http_outgoing.js:450:15)
    at Request.ondata (internal/streams/legacy.js:16:26)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:189:7)
    at IncomingMessage.<anonymous> (/Users/knowlet/test/node_modules/request/request.js:1088:12)
    at emitOne (events.js:96:13)
    at IncomingMessage.emit (events.js:189:7)
    at IncomingMessage.Readable.read (_stream_readable.js:381:10)
    at flow (_stream_readable.js:761:34)
    at resume_ (_stream_readable.js:743:3)

And req.pipe(request(...)).pipe(res) works in express.js, I’m wondering is there anything I do wrong?

$ node -v
v7.6.0

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

39reactions
PlasmaPowercommented, Mar 18, 2017

You’ll want to let Koa handle the stream:

ctx.body = ctx.req.pipe(request(`https://www.google.com/?q=${ctx.query.q}`));
18reactions
catamphetaminecommented, Oct 7, 2017

you can use ctx.respond = false

Oh, you have that. Ok, that will do.

However, that is not the recommended solution, and it may cause problems with other middleware.

How about this

.use(async (ctx) =>
{
	// https://medium.com/@aickin/whats-new-with-server-side-rendering-in-react-16-9b0d78585d67
	ctx.res.write(header)
	await pipe(stream, ctx.res, { end: false })
	ctx.res.write(footer)
	ctx.res.end()
})

// Pipes `from` stream to `to` stream.
// Returns a `Promise`.
function pipe(from, to, options)
{
	return new Promise((resolve, reject) =>
	{
		from.pipe(to, options)
		from.on('error', reject)
		from.on('end', resolve)
	})
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to render response after stream finished pipe?
createReadStream(fileName).pipe(ctx.res); await streamEnd(readStream); ctx.status = HttpStatus.OK; } else { console.error(`File ${fileName} ...
Read more >
koajs/koa - Gitter
Koa understands that it should pipe the stream set to ctx.body to the response ... i want to set a session for 2...
Read more >
Node.js v19.3.0 Documentation
Creates and returns a new Blob containing a subset of this Blob objects data. The original Blob is not altered. blob.stream() #. Added...
Read more >
Action-ök | Moleculer - Progressive microservices framework for ...
const res = await broker.call(actionName, params, opts); ... Access it via ctx.meta in actions handlers. It will be transferred & merged ... stream.pipe(s);...
Read more >
Using readable streams - Web APIs | MDN
Browser support. You can consume Fetch body objects as streams and create your own custom readable streams most current browsers. Pipe chain ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found