title: Passing a JSON object from clientside JavaScript to NodeJS
body: <p>I have a webpage which creates a JSON object based on user input. I would like to then somehow allow the user to submit this JSON object to a NodeJS script for processing/insertion into a MySQL database. However, I'm really not sure how to do something like this -- the best I can come up with is some form of a POST, but I'm not sure where to start with this. </p>
  
  <p>Because I don't know what such a method would be described as, I haven't had much success in locating any tutorials or other resources online. </p>
  
  <p>Could anyone suggest some articles or documentation to look at that would be relevant to something like this? Or, at least, tell me what to search for? Thanks. </p>
  
  <p>EDIT: This is the code I am trying to get working at the moment. I'm just trying to convert the POST data type from string to JSON on both sides.</p>
  
  <p>Serverside:</p>
  
  <pre><code>var express = require('express');
  var fs = require('fs');
  var app = express();
  
  app.use(express.bodyParser());
  
  app.get('/', function(req, res){
      console.log('GET /')
      //var html = '&lt;html&gt;&lt;body&gt;&lt;form method="post" action="http://localhost:3000"&gt;Name: &lt;input type="text" name="name" /&gt;&lt;input type="submit" value="Submit" /&gt;&lt;/form&gt;&lt;/body&gt;';
      var html = fs.readFileSync('index.html');
      res.writeHead(200, {'Content-Type': 'text/html'});
      res.end(html);
  });
  
  app.post('/', function(req, res){
      console.log('POST /');
      console.dir(req.body);
      res.writeHead(200, {'Content-Type': 'text/html'});
      res.end('thanks');
  });
  
  port = 8080;
  app.listen(port);
  console.log('Listening at http://localhost:' + port)
  </code></pre>
  
  <p>Clientside: </p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
      &lt;form method="post" action="http://localhost:8080"&gt;
          Name: &lt;input type="text" name="name" /&gt;
          &lt;input type="submit" value="Submit" /&gt;
      &lt;/form&gt;
  
      &lt;script type="text/JavaScript"&gt;
          console.log('begin');
          var http = new XMLHttpRequest();
          var params = "text=stuff";
          http.open("POST", "http://localhost:8080", true);
  
          http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          //http.setRequestHeader("Content-length", params.length);
          //http.setRequestHeader("Connection", "close");
  
          http.onreadystatechange = function() {
              console.log('onreadystatechange');
              if (http.readyState == 4 &amp;&amp; http.status == 200) {
                  alert(http.responseText);
              }
              else {
                  console.log('readyState=' + http.readyState + ', status: ' + http.status);
              }
          }
  
          console.log('sending...')
          http.send(params);
          console.log('end');
  
      &lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>