title: Basic Ajax send/receive with node.js
body: <p>So I'm trying to make a very basic node.js server that with take in a request for a string, randomly select one from an array and return the selected string.  Unfortunately I'm running into a few problems.</p>
  
  <p>Here's the front end:</p>
  
  <pre><code>function newGame()
  {
     guessCnt=0;
     guess="";
     server();
     displayHash();
     displayGuessStr();
     displayGuessCnt();
  }
  
  function server()
  {
     xmlhttp = new XMLHttpRequest();
  
     xmlhttp.open("GET","server.js", true);
     xmlhttp.send();
  
     string=xmlhttp.responseText;
  }
  </code></pre>
  
  <p>This should send the request to server.js:</p>
  
  <pre><code>var http = require('http');
  
  var choices=["hello world", "goodbye world"];
  
  console.log("server initialized");
  
  http.createServer(function(request, response)
  {
      console.log("request recieved");
      var string = choices[Math.floor(Math.random()*choices.length)];
      console.log("string '" + string + "' chosen");
      response.on(string);
      console.log("string sent");
  }).listen(8001);
  </code></pre>
  
  <p>So clearly there are several things going wrong here:</p>
  
  <ol>
  <li><p>I get the feeling the way I am "connecting" these two files isn't correct both in the <code>xmlhttp.open</code> method and in using <code>response.on</code> to send the string back to the front end.</p></li>
  <li><p>I'm a little confused with how I call this page on localhost.  The front end is named index.html and the sever posts to 8001.  What address should I be go to on localhost in order to access the initial html page after I have initialized server.js? Should I change it to <code>.listen(index.html)</code> or something like that?</p></li>
  <li><p>are there other obvious problems with how I am implementing this (using <code>.responsetext</code> etc.)</p></li>
  </ol>
  
  <p>(sorry for the long multi-question post but the various tutorials and the node.js source all assume that the user already has an understanding of these things.)</p>