title: Nodejs server with Express not sending responses to HTTP requests from javascript file, but is receiving the requests and processing them
body: <p>So I have a node-js server and an apache server on the same machine, and one of the javascript files is sending an HTTP request to the node-js server. The node-js server receives the file, reads the data, puts it in the database, as it should, but it isn't sending back any status codes or data.</p>
  
  <p>Here is the XHTMLRequest send code snippet,</p>
  
  <pre><code>// creates a new http request to be sent to the nodejs server
  function createNewUser(username, password, email) {
  
      // The url is the URL of our local nodejs server
      var userCreateRequest = new XMLHttpRequest();
      userCreateRequest.open( "POST", "http://&lt;machine's IP&gt;:8080/api/users" );
  
      // Create json object for user data
      var user = "name="+username+"&amp;password="+password+"&amp;email="+email;
      alert(user);
  
      // set content type for http request
      userCreateRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  
      // Event listern for server response
      // userCreateRequest.addEventListener("readystatechange", processRequest, false);
  
      // Call process request whenever state changes
      userCreateRequest.onreadystatechange = function() {
              alert(this.readyState + ", " + this.status);
              if (this.readyState == 4 &amp;&amp; this.status == 200) {
  
                      var response = this.response;
                      alert(response.name);
  
              }
  
      }
  
      // Send user data to server
      userCreateRequest.send(user);
  
  }
  </code></pre>
  
  <p>And here is the code for the node-js server (with express)</p>
  
  <pre><code>router.route('/users')
  
      .post(function(req, res) { //create a new user
  
              var user = new User();
  
              user.name = req.body.name;
              user.password = req.body.password;
              user.email = req.body.email;
  
              user.save(function(err) { //add user object to database
  
                      if(err)
                              res.send(err);
  
                      res.status(200).json(user);
  
              });
  
      });
  </code></pre>
  
  <p>As I said above, the code works fine in terms of putting the body of the request in the database and what-not, but the server is not sending back the 200 OK response (or I'm failing to receive it for some reason). The only times I get an alert from onreadystatechange is when it's state 2, status 0, and state 4, status 0.</p>