title: Node.js Create new Routes from TCP Data
body: <p>I have the following code, which is meant to capture JSON data through TCP, and then create a new route based on whatever is in the JSON file. So If one of my JSON files has:</p>
  
  <pre><code>{"pagename": "singsong"} 
  </code></pre>
  
  <p>Then I want my route to be <code>mywebsite:8000/singsong</code> and contain all data from whatever has the <code>singsong</code> pagename. </p>
  
  <p>The problem I am facing is that my TCP data gets sent to all routes. So my route <code>mywebsite:8000/singsong</code> will contain JSON data that has <code>{"pagename": "hazel"}</code>, when I am trying to create new routes for each data. </p>
  
  <p>My code, as is:</p>
  
  <pre><code>server.on("connection", function(socket){
      chunk = "";
  socket.on('data', function(data){
      chunk += data.toString(); // Add string on the end of the variable 'chunk'
      d_index = chunk.indexOf(';'); // Find the delimiter
  
      // While loop to keep going until no delimiter can be found
      while (d_index &gt; -1) {         
          try {
              string = chunk.substring(0,d_index); // Create string up until the delimiter
              json = JSON.parse(string); // Parse the current string
              app.set('fee', data);
              app.set('dee', json);
              console.log(json.pagename); // Function that does something with the current chunk of valid json.    
                  app.get("/"+json.pagename, function(req, res){
                      res.writeHead(200, {'Content-Type': 'text/plain'});
                      res.write(JSON.stringify(req.app.get('dee')));
                      res.end();
                  });
          }
          catch(e){
              console.log(e);
          }
          chunk = chunk.substring(d_index+1); // Cuts off the processed chunk
          d_index = chunk.indexOf(';'); // Find the new delimiter
      }      
  });
          socket.on("close", function(){
              console.log("connection closed");
          });
  });
  </code></pre>