Friday, 26 May 2017

Type of a data chunk in Node http request

I am studying code here of setting up a simple Node server. I have seen and used many times this idiom of saving data chunks in an array and finally concatenating them together.

http.createServer(function(request, response) {
  var body = [];
  request.on('data', function(chunk) { body.push(chunk); });
  request.on('end', function() { body = Buffer.concat(body).toString();
  ...

  1. What is the type of chunk? Documentation says it's eitherBuffer or string, but which one?

  2. Is it safe to call Buffer.concat(body) where body is an array of strings? Documentation of Buffer.concat(list) says list should be a list of Buffer instances. Are strings "Buffer instances"?



via sam

No comments:

Post a Comment