var server = net.createServer(function(c) {
//...
c.on('data', function(data) {
//The data is all data, but what if I need only first N and do not need other data, yet.
c.write(data);
});
//...
};
Is there a way to read only defined portion of data? For example:
c.on('data', N, function(data) {
//Read first N bytes
});
Where N is number of bytes I expect. So the callback gets only N out of M bytes.
The solution is (thanks to mscdex):
c.on('readable', function() {
var chunk,
N = 4;
while (null !== (chunk = c.read(N))) {
console.log('got %d bytes of data', chunk.length);
}
});
Readable streams in node v0.10+ have a read()
that allows you to request a number of bytes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With