Q & A: buffering streams
Question: when and how do you need to explicitly close a stream?
Generally, you don't need to. To close, it depends on the implementation details of the stream you're dealing with.
Send back an error instead of ending the stream:
var concat = require('concat-stream')
var through = require('through')
var http = require('http')
var qs = require('querystring')
var server = http.createServer(function (req, res) {
req
.pipe(through(counter))
.pipe(concat({ encoding: 'string' }, onbody))
function counter () {
var size = 0
return through(function(buf, enc, next) {
size += buf.length
if (size > 20) {
// **** 2. ↴
res.end('ETOOBIG\n')
// **** 1. send back an error instead ↴
// next(null, null)
} else {
next(null, buf)
}
})
}
function onbody (body) {
var params = qs.parse(body)
console.log(params)
res.end('ok\n')
}
})
server.listen(5000)
No we actually get an error back when the message is too big.
Question: so you just can call
res.end()
and not worry about next?
If we never call next()
, we're never pulling more data from the stream.