0

I do not have much experience with javascript and I am following a node.js book and in it there is the following code

function start(route, handle)
{
  function onRequest(request, response)
  {
      var postdata='';
      var pathname=url.parse(request.url).pathname;

      request.setEncoding('utf8'); 

      request.addListener("data", function(postDataChunkd)
      {
         postdata+=postDataChunkd;
      });

      console.log('Post data: ' + postdata);// <----------------------- HERE

      request.addListener("end", function()
      {
         route(handle, pathname, response, postdata);  

      }); 
}

http.createServer(onRequest).listen(8124);

console.log('Server running on 8124');
}

exports.start=start;

The code is working as expected however the thing is that for me it seems a bit obscure that the variable postData is logged as the empty string in the middle of addListener("data"... and addListener("end"...

Could someone explain why the postData variable is logged empty despite a successful request?

Regards

Dan

  • First order of business when using NodeJS is to get familiar with the concept of asynchronous programming. You're adding a listener for the `data` event, and clearly that event has not happened yet when your `console.log()` runs. –  Oct 09 '14 at 17:13
  • ...put the logging *inside* the event handlers and you'll get a meaningful result. –  Oct 09 '14 at 17:14
  • Definitely I will put more attention to the asynchronous side. Thanks – user3256539 Oct 09 '14 at 19:05

2 Answers2

2

Could someone explain why the postData variable is logged empty despite a successful request?

It's actually nothing to do with closures, just timing. As of when it's logged, the request hasn't completed (or even gotten a single "data" callback) yet, so the variable hasn't had anything assigned to it. The request completes asychronously.

Here's the order in which the things in that onRequest callback occur:

  1. The variables postdata and pathname are created.

  2. postdata is set to ''.

  3. pathname is set to the result of evaluating url.parse(request.url).pathname.

  4. request.setEncoding is called.

  5. The function being passed into the first call to request.addListener is created.

  6. request.addListener is called and passed the "data" string and the function created in the last step. The function is not executed at this time.

  7. The expression 'Post data: ' + postdata is evaluated, yielding 'Post data: ' (as postdata is ''), and passed into console.log to be written to the console.

  8. The function being passed to the second call to request.addListener is created.

  9. request.addListener is called with "end" and the function created in the last step.

  10. Some time later, there may or may not be one or several calls to the function creaetd in Step 6.

  11. Some time later, there will probably be one call to the function created in step 8.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Node.js does not wait for the addListener function to finish. Node will start up the asynchronous function then move on to the next line of code. At the time of execution postdata = ''.

Travis Pettry
  • 1,220
  • 1
  • 14
  • 35