title: Node js "error:write after end" using fs
body: <p>Ive got a piece of code running in node js that reads incoming data over sockets. the information is displayed on the console which is useful but I want to also write it to a text file for further analysis. 
  However as I had the code configured it worked but just kept overwriting the contents of the file. 
  Ive changed it about to try and make sure the text file isnt constantly being overwritten. I now get the "write after end" error. Ive attached the code below. I would like it if the raw data in the variable "data" was saved in a text file. it would be great.
  Thanks guys</p>
  
  <pre><code>Load the TCP Library
  
  var net = require('net');
      var fs = require('fs');
  // Keep track of the chat clients
  var clients = [];
  var tcpport = 13000;
  var tcphost;
  var wstream = fs.createWriteStream('myOutput.txt');
  // Start a TCP Server
  net.createServer(function (socket) {
  
    // Identify this client
    socket.name = socket.remoteAddress + ":" + socket.remotePort;
  
    // Put this new client in the list
    clients.push(socket);
  
    // Send a nice welcome message and announce
    console.log("Welcome " + socket.name);
    console.log(socket.name + " joined");
  
    // Handle incoming messages from clients.
    socket.on('data', function (data) {
      console.log('Incoming Data: ' + data.toString());
  
  
  wstream.write(data.toString());
  
  wstream.end();
      var str = data.toString().split("@");
      if (str[1] == 'Pong'){
          broadcast('AnnouncerKit@AckPong@$');
              broadcast('AnnouncerKit@Ping@$');
      }
      if (str[1]== 'AckPing'){
          broadcast('AnnouncerKit@GetInfo@$');
      }
      if (str[1]== 'Store'){
          broadcast('AnnouncerKit@AckStore@'+str[str.length-2]+'@$');
          //console.log(bibs);
  
  
      }
    });
  
    // Remove the client from the list when it leaves
    socket.on('end', function () {
      clients.splice(clients.indexOf(socket), 1);
      console.log(socket.name + " left");
    });
  
    // Send a message to all clients
    function broadcast(message) {
      clients.forEach(function (client) {
        client.write(message);
      });
      // Log it to the server output too
      console.log('Outgoing Data: ' + message);
    }
  
  }).listen(tcpport);
  
  console.log("TCP server running at port " + tcpport);
  enter code here
  </code></pre>
  
  <p>Also just for reference, here is the working code but it overwrites.</p>
  
  <pre><code>// Load the TCP Library
  var net = require('net');
  
  // Keep track of the chat clients
  var clients = [];
  var tcpport = 13000;
  var tcphost;
  
  // Start a TCP Server
  net.createServer(function (socket) {
          var fs = require('fs');
    // Identify this client
    socket.name = socket.remoteAddress + ":" + socket.remotePort;
  
    // Put this new client in the list
    clients.push(socket);
  
    // Send a nice welcome message and announce
    console.log("Welcome " + socket.name);
    console.log(socket.name + " joined");
  
    // Handle incoming messages from clients.
    socket.on('data', function (data) {
      console.log('Incoming Data: ' + data.toString());
      var wstream = fs.createWriteStream('myOutput.txt');
  wstream.write(data.toString());
  
  wstream.end();
      var str = data.toString().split("@");
      if (str[1] == 'Pong'){
          broadcast('AnnouncerKit@AckPong@$');
              broadcast('AnnouncerKit@Ping@$');
      }
      if (str[1]== 'AckPing'){
          broadcast('AnnouncerKit@GetInfo@$');
      }
      if (str[1]== 'Store'){
          broadcast('AnnouncerKit@AckStore@'+str[str.length-2]+'@$');
          //console.log(bibs);
  
  
      }
    });
  
    // Remove the client from the list when it leaves
    socket.on('end', function () {
      clients.splice(clients.indexOf(socket), 1);
      console.log(socket.name + " left");
    });
  
    // Send a message to all clients
    function broadcast(message) {
      clients.forEach(function (client) {
        client.write(message);
      });
      // Log it to the server output too
      console.log('Outgoing Data: ' + message);
    }
  
  }).listen(tcpport);
  
  console.log("TCP server running at port " + tcpport);
  </code></pre>
  
  <p>And the resulting output in the text file</p>
  
  <pre><code>Finish@Store@000064919:13:36.505 1 03  155641803211E@000089619:13:36.826 1 0F  155661803213D@683@$
  </code></pre>