title: AJAX call to/from MongoDB example for Node/Express?
body: <p><em>This is to start with a very basic page: HTML Form, a button, and a div-box.</em></p>
  
  <p>.click of the button would POST the Form data through AJAX.</p>
  
  <p>The data is to be stored in MongoDB, and retrieved into the div-box without a page-refresh.</p>
  
  <p><em>AJAX from index.html:</em></p>
  
  <pre><code>$(document).ready(function()
  {   
  
      // handle button clicks         
      $('#buttonID').click(function() {
  
          // make an ajax call
          $.ajax({
              dataType: 'jsonp',
              jsonpCallback: '_wrapper',
              data: $('#formID').serialize(),
              type: 'POST',
              url: "http://localhost:9999/start",
              success: handleButtonResponse,
              });
      });
  
      function handleButtonResponse(data)
      {
          // parse the json string
          var jsonObject = JSON.parse(data);
          $('#reponseID').append( jsonObject.message );
      }
  
  });
  </code></pre>
  
  <p><em>app.js:</em></p>
  
  <pre><code>var express = require('express'),
      app = express();
      cons = require('consolidate');
      MongoClient = require('mongodb').MongoClient,
      Server = require('mongodb').Server;
  
  app.engine('html', cons.swig);
  app.set('view engine', 'html');
  app.set('views', __dirname + "/views");
  
  var mongoclient = new MongoClient(new Server('localhost', 27017,
                                                  { 'native_parser' : true }));
  
  var db = mongoclient.db('database_name');
  
  app.get('/', function (req, res) {
  
      db.collection('collectionName').find({}, function (err, doc) {
              res.render('index', doc);   
      });
  
      response.writeHead(200, {"Content-Type:": "application/json"}); 
      var submittedPost = {};
      submittedPost['message'] = 'Proof that Node and Mongo are working..';
      response.write( "_wrapper('" );
      response.write( JSON.stringify(submittedPost) );
      response.write( "')");              
      response.end();
  
  });
  
  app.get('*', function (req, res) {
      res.send("404..", 404);
  });
  
  mongoclient.open(function (err, mongoclient) {
  
      if (err) throw err
  
      app.listen(9999);
      console.log("Express server started on port 9999");
  });
  </code></pre>
  
  <p><strong>How/Where does the JSON connect to/from MongoDB?</strong></p>
  
  <p>Also, does Express require a templating engine, such as Consolidate? If so, how/where does that fit in?</p>