title: How do I use client side JavaScript to find and return data from MongoDB and a Node.js server with no framework?
body: <p>I have read all the questions on SO that I could find.  They all use Express, Mongoose or they leave something out.  I understand that Node.js is the server.  I understand the MongoDB <code>require</code> is the driver the Node.js server uses to open a connection to the MongoDB.  Then, on the server, I can do (from the <a href="https://docs.mongodb.com/getting-started/node/query/" rel="nofollow noreferrer">documentation</a>):</p>
  
  <pre><code>var MongoClient = require('mongodb').MongoClient;
  var assert = require('assert');
  var ObjectId = require('mongodb').ObjectID;
  var url = 'mongodb://localhost:27017/test';
  
  var findRestaurants = function(db, callback) {
     var cursor =db.collection('restaurants').find( );
     cursor.each(function(err, doc) {
        assert.equal(err, null);
        if (doc != null) {
           console.dir(doc);
        } else {
           callback();
        }
     });
  };
  // Connect to the db
  MongoClient.connect(url, function(err, db) {
    assert.equal(null, err);
    findRestaurants(db, function() {  //I don't want to do this as soon as the server starts
        db.close();
    });
  });
  
  //if I put findRestaurant here,
  function findRestaurant(data){
  
  }
  </code></pre>
  
  <p>How do I call it from the client?
  I do not want to find data as soon as I start the server.  I realize those are examples, but what I cannot find is a way where the client requests some data and where the Node.js server returns it.</p>
  
  <p>I have seen close examples using jQuery, Angular on the client, and then Express, Mongoose, Meteor, , etc.</p>
  
  <p>All I want to understand is how I make this request from the client's browser.  I can do that with XMLhttpRequest(), so I can put that part together, I believe.  But, any example is appreciated.</p>
  
  <p>But what is waiting on the Node.js side of things (how do I set up my function to be called once the server is listening)?</p>
  
  <p>How do I create a function on the server side, maybe "GetRestaurants" and have that return the data it gets using find()?</p>
  
  <p>I cannot find this information, this simple, anywhere.  Is it too complicated to do the example without a framework?</p>
  
  <p>I do not wish to copy and paste from something using Express, etc. without understanding what's going on.  Most explanations never say, this goes on the Node.js side.  This is client.  I know I am expected to do my own research, but I am not putting it together, too used to RDBMSes, IIS, Apache, PHP, and so on.</p>
  
  <p>I believe I have a fundamental misunderstanding of what's going on in the paradigm.</p>
  
  <p><strong>Please</strong>.  No REST API creation, no frameworks of any kind on Node.js other than using the MongoDB library (unless there is an absolute <em>requirement</em>), not even jQuery, Angular, Jade, or anything else for the client side, straight up JavaScript on all sides.</p>
  
  <p>I have seen questions like this,</p>
  
  <p><a href="https://stackoverflow.com/questions/21053577/how-to-display-data-from-mongodb-to-the-frontend-via-node-js-without-using-a-fra">How to display data from MongoDB to the frontend via Node.js without using a framework</a></p>
  
  <p>But they do not show what I am asking.  They do it all at once, as soon as the database connects.  What if I want to do a delete or insert or find?  There are many SO questions like this, but I have not hit the one that shows what I am looking for.</p>