Thursday 13 April 2017

Trouble using res.end() in a node server

so, below is a code snippet from my server.js file. When running, and I send a URL with a message, the res.end() causes the view to render a blank page.

When I comment out the res.end() command, the view displays all of the messages, but the browser waits and waits for the signal that the response from the server is complete.

I get that you can use res.end() and put data in the parens, to be transmitted and rendered by the view.

What I expect to happen is that with no args, it will just leave the view alone, but the empty args in the parens is manifesting as an empty view.

How do I indicate that the response is complete without deleting the data on the view?

if(url_parts.pathname.substr(0, 5) == '/msg/') {
  // message receiving
  var msg = unescape(url_parts.pathname.substr(5));
  messages.push(msg);
  while(clients.length > 0) {
    var client = clients.pop();
    client.end(JSON.stringify({
      count: messages.length,
      append: msg+"\n"
    }));
  }
  res.end(); //if left in, this renders an empty page, if removed,
  // client keeps waiting....
  }
  }).listen(8080, 'localhost');

xx



via Captain Chaos

No comments:

Post a Comment