Adding the application code
Now, let's write the simple application code to test things out. This app is going to basically connect to our locally running MongoDB server, insert a few records as seed data, and then provide the output on whether or not the data was inserted properly into MongoDB.
You can download a Gist of the code via the URL: http://bit.ly/1JpT8QL.
Using your editor of choice, create a new file named app.js
and save it to the application root, which is the testapp
folder. Just copy the content of the above Gist on to the app.js
file.
Understanding the code
Now, let's go through and explain what each section of the code is doing.
//require the mongoClient from mongodb module var MongoClient = require('mongodb').MongoClient;
The preceding line requires the MongoDB Node driver that we installed via npm. This is the required convention used in Node.js for bringing in external file dependencies to the current file in context. We will explain more about this...