title: Problems sending query results over TCP in a nodejs client/server
body: <p>I'm trying to make a sort of middleman between my database and other node projects. I realize my code is probably garbage but I'm working on a uni project and have deadlines.</p>
  
  <p>Server code:</p>
  
  <pre><code>const net = require('net');
  const port = 7070;
  const host = '127.0.0.1';
  const mariadb = require('mariadb/callback')
  
  const conn = mariadb.createConnection({
      host: 'localhost',
      port: '3306',
      user: 'root',
      password: '',
      database: 'ecomm'
  });
  
  const server = net.createServer();
  server.listen(port, host, () =&gt; {
      console.log('API server is running on port ' + port + '.');
      //console.log(conn);
  
  });
  
  let sockets = [];
  server.on('connection', function(sock) {
      console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);
  
  
      sockets.push(sock);
  
      sock.on('data', function(data) {
          console.log('DATA ' + sock.remoteAddress + ': ' + data);
          if (data == 'fetch') {
              console.log("Fetch operation requested");
              conn.query("SELECT item_id, item_name, item_price, held_by from items", (err, rows) =&gt; {
                  if (err) console.log("DB connection failed: " + err);
                  else {
                      console.log(rows[0].item_price);
                      //sock.write('hehe');
                      var i;
                      for (i = 0; i &lt; 5; i++) {
                          sock.write(JSON.stringify(rows[i]));
                          //sock.write(rows[i]);
                      }
  
                  }
              });
          }
          // Write the data back to all the connected, the client will receive it as data from the server
  
      });
  
      // Add a 'close' event handler to this instance of socket
      sock.on('close', function(data) {
          let index = sockets.findIndex(function(o) {
              return o.remoteAddress === sock.remoteAddress &amp;&amp; o.remotePort === sock.remotePort;
          })
          if (index !== -1) sockets.splice(index, 1);
          console.log('CLOSED: ' + sock.remoteAddress + ' ' + sock.remotePort);
      });
  });
  
  process.on('uncaughtException', function (err) {
      console.error(err);
      //console.log("Client disconnected");
    });
  </code></pre>
  
  <p>Client code:</p>
  
  <pre><code>const net = require('net');
  const client = new net.Socket();
  const port = 7070;
  const host = '127.0.0.1';
  
  
  client.connect(port, host, function() {
      console.log('API connection successful.');
      client.write('fetch');
  
  });
  client.on('data', function(data) {
      //console.log('Server Says : \n' + JSON.parse(data));
      var parsed_data = JSON.parse(data);
  
      console.log(parsed_data);
  });
  
  client.on('close', function() {
      console.log('Connection closed');
  });
  </code></pre>
  
  <p>Now the server, upon getting a fetch request, will get all items from the database, and then throw it back to the client. The problem is, if I send 1 or 2 items; it works fine. If I send more than that, I get a parsing error and the client crashes.</p>
  
  <pre><code>    SyntaxError: Unexpected token { in JSON at position 64
      at JSON.parse (&lt;anonymous&gt;)
      at Socket.&lt;anonymous&gt; (C:\Users\saif\Desktop\CliServTest\client.js:14:2
      at Socket.emit (events.js:198:13)
      at addChunk (_stream_readable.js:288:12)
      at readableAddChunk (_stream_readable.js:269:11)
      at Socket.Readable.push (_stream_readable.js:224:10)
      at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
  </code></pre>