Setup/Configure NodeJS, MongoDB, Mongoose with Visual Studio Code

The intent of the article is to provide a step-by-step instruction to write a “Hello World” application using NodeJS, MongoDB, Mongoose in “Visual Studio Code” editor.  The article also assumes that you are aware of the concepts behind using a MEAN stack for developing JavaScript based web application and their advantages etc.,

Step 1 (Install Node):

Download and Install NodeJS from this link

Step 2 (Install Mongo DB):

Download and Install Mongo DB from this link (make sure that you follow the instructions on the page to setup the Windows service and test it, before proceeding further).

Step 3 (Setup development environment):

  1. Fire up “Visual Studio Code” and use the “Open Folder” option to point to “any” folder where you plan to create/store the project specific files (to be created as part of the “hello world” application).
  2. Use the “File-> New File” option (or the “New File” in the Explorer window) to create a new file and name it as “main.js“.
  3. Launch the “Integrated Terminal” window from “View->Integrated Terminal”.
  4. Type “npm init” (as provided in the below image and keep pressing “enter” for all questions prompted to assign the default values, which can be overridden by providing a custom value, if preferred). integratedterminal
  5. When completed it will create the “package.json” file along with all the default settings.
  6. Open up the “package.json” and add the dependencies for mongodb and mongoose (as highlighted below) and save the file:packagejson
  7. In the “Integrated Terminal” type “npm install” and hit enterdependencyinstall
  8. After the dependencies are installed by npm, you should see the “node_modules” folder on the explorer window (which contains all the dependent npm modules required for mongodb and mongoose to run in nodejs ecosystem):nodemodules


Step 4 (Hello World App):

Now that we are done with the configuration/setup of the development environment, its time to write our basic “hello world” code:

var mongoose = require('mongoose');

mongoose.connect('mongodb://127.0.0.1/test');

//DEFINE THE SCHEMA
var catSchema = mongoose.Schema({
    name: String
});

//GENERATE THE MODEL OBJECT
var Cat = mongoose.model('Cat', catSchema);

//CREATE AN INSTANCE OF THE MODEL WITH DATA
var kitten = new Cat({name: 'jimmy'});

//PERFORM A SAVE OPERATION
kitten.save(function(err){
    if (err){
        console.log(err);
    }
    else{
        console.log('meow');

        //UPON SUCCESSFUL SAVE, USE THE FIND API TO QUERY FOR THE SAVED DATA
        var catResult = mongoose.model('Cat', catSchema);

        catResult.find({ name: 'jimmy'}, function(err, catResult){
            if (err){
                console.log(err);
            }
            else{
                console.log(catResult);
            }
        });
    }
});

Step 5(Run/Execute the code):

  • Before we can run the code in Visual Studio code we need to provide some more details to the editor in the form of “launch.json” to let know Visual Studio code on the type to application, port etc.,
  • Hit “F5” to start the “execution” of the code. Now this will bring up a dropdown window with 3 options to select the appropriate environment.  Select “Node.js” :nodedebugenvironment
  • Goto “View->Debug Console” to open up the debug output window (which will be loaded with all the console.log statements in the main.js).
  • Now hit “F5” to run the code.
  • You should see the output as below :finaloutput

 

970 total views, 10 views today

0 votes

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha *