Adventures in MEAN Stack World

Nothing lasts forever. So if you want a library to last forever, technology is not the space for you - John Papa

Basics of Node.js for MEAN Stack Development

Introduction

  • This is just an introduction of Node.js.
  • In other words 'N' of the MEAN Stack.
  • This is the second part of the series of articles which I will write about the MEAN.
  • Here is the first part of this article MEAN Stack Development with MongoDB.


What is Node.js ?

  • Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications.
  • It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
  • It's perfect for data-intensive real-time applications that run across distributed devices.

How to Install Node.js on Windows ?

Step 1 :

  • Installing Node.js on a Windows machine is a simple task that can be easily accomplished using the standalone installer.
  • Just go to Node.js download page and download the right .msi file (32-bit or 64-bit ).
  • I have downloaded the 64-bit version.

Step 2 :
  • After downloading the installer, just double click it.
  • If you get any security dialog boxes, just click on the Run button and the installation wizard should start.
  • You will be prompted with an installation screens similar to the following screenshots.




Congratulations !. You have successfully installed the Node.js.

How to Run Node.js ?

Step 1 :
  • You have to open the Nods.js Command Line Interface (CLI).
  • Just go to the Nods.js installed folder and run the Node application as shown below.

  • This will start the Node.js CLI, which will wait for a JavaScript input. 

Step 2 :

  • To test the Node.js installation, run the following command on CLI.
          console.log('Node is up and running!');


  • Congratulations ! You have just created your fist Node.js application. 

Step 3 :
  • To stop the CLI, press CTRL + D

What is  Node Package Manager (NPM) ?

  • Node.js is a platform, which means its features and APIs are kept to a minimum.
  • To achieve more complex functionality, it uses a module system that allows you to extend the platform.
  • The best way to install, update, and remove Node.js modules is using the NPM.
  • Conveniently, NPM is installed during the Node.js installation process.

What are the Key  Features of NPM ?

  • A registry of packages to browse, download, and install third-party modules.
  • Command Line Interface (CLI) tool to manage local and global packages.


What are the Installation modes on NPM ?

  • It is important to remember that NPM has two installation modes.
  • Which are Local mode and Global mode.

Local Mode
  • The default local mode is used more often and installs the third-party packages in a local node_modules folder placed inside your application folder.
  • It has no effect system-wise, and is used to install the packages your application needs, without polluting your system with unnecessary global files.

Global Mode
  • The global mode is used to install packages you want Node.js to use globally.
  • Usually these are CLI tools, such as Gulp.
  • Most of the time, the package author will specifically instruct you to install the package globally. Therefore, whenever in doubt, use the local mode.
  • The global mode will usually install the packages in the C:\Users\%USERNAME%\AppData\Roaming\npm\ node_modules folder for Windows-based systems, making it available to any Node.js application running on the system.

How to Install a package using NPM ?


Step 1 :
  • Once you find the right package, you'll be able to install it using the npm install command as follows.

Local Mode :

  • Run 'Command Prompt' with “Administrative Privileges”.
  • Go to your Node.js App's root folder.
          npm install <Package Unique Name>


      e.g.        
          D:\Freelance Work\My Blog\Mean Introduction\NodeJs\Test>npm install express


  • The preceding command will install the latest stable version of the Express package in your local node_modules folder as follows. 

  • When a package has dependencies, NPM will automatically resolve those dependencies, installing the required packages in a node_modules folder inside the package folder as follows.


Global Mode :
  • Run 'Command Prompt' with “Administrative Privileges”.
  • Installing a module globally is similar to its local counterpart, but you'll have to add the –g flag as follows.
          npm install –g <Package Unique Name>

      e.g. 
            D:\Freelance Work\My Blog\Mean Introduction\NodeJs\Test>npm install -g express


  • The preceding command will install the latest stable version of the Express package in your Roaming folder as follows. 
  • Note that the "AppData" folder is Hidden by default. 



How to Remove a package using NPM ?

  • To remove an installed package, you have to navigate to your application folder and run the following command according to the mode.

Local Mode :
         npm uninstall < Package Unique Name>

  e.g.        
          D:\Freelance Work\My Blog\Mean Introduction\NodeJs\Test>npm uninstall express

Global Mode :
        npm uninstall –g < Package Unique Name>

  e.g.        
          D:\Freelance Work\My Blog\Mean Introduction\NodeJs\Test>npm uninstall –g express


How to Update a package using NPM ?

  • To update a package to its latest version, issue the following command according to the mode.

Local Mode :
         npm update < Package Unique Name>

  e.g.        
          D:\Freelance Work\My Blog\Mean Introduction\NodeJs\Test>npm update express

Global Mode :
        npm update –g < Package Unique Name>

  e.g.        
          D:\Freelance Work\My Blog\Mean Introduction\NodeJs\Test>npm update –g express


What is a package.json file ?

  • The package.json file is basically a JSON file that contains the different attributes you'll need to describe about your application properties.
  • Your application will need to use several packages, and so you'll need a better way to manage these package dependencies.
  • For this purpose, NPM allows you to use a configuration file named package.json in the root folder of your application.
  • In your package.json file, you can define various metadata properties of your application, including properties such as the name, version, and author of your application. 
  • This is also where you define your application dependencies.
  • Let's say our application is using the Express and Gulp packages.So then we will have a package.json file as follows.
                           {
                               "name" : "MEAN App",
                               "version" : "0.0.1",
                               "author" : "Sampath Lokuge", 
                               "dependencies" : {
                                     "express" : "latest",
                                     "gulp" : "latest"
                                    }
                           }
            
Important Note :
  • Your application name and version properties are required.
  • If you remove these properties then NPM will not work properly.
  • The "latest" keyword to tell NPM to install the latest versions of the packages. 


How to Create a package.json file ?

  • Go to your Node.js App's root folder.
          npm init
e.g.        
          D:\Freelance Work\My Blog\Mean Introduction\NodeJs\Test>npm init

  • NPM will ask you a few questions about your application and will automatically create a new package.json file for you.
  • A sample screenshot as follows.


How to Install the package.json dependencies ?

install :
  • If you need to install Gulp,then change the package.json file's dependencies section as follows.
           "dependencies": {
                    "express": "^4.11.2",
                    "gulp": "latest"
            }
  • In the preceding code example, I have used the "latest" keyword to tell NPM to install the latest versions of the Gulp package. 
  • Navigate to your application's root folder and use the "npm install" command as follows.
          npm install

  e.g.        
          D:\Freelance Work\My Blog\Mean Introduction\NodeJs\Test>npm install


  • NPM will automatically detect your package.json file and will install all your application dependencies.
  • You can find out them under a local "node_modules" folder. 

update :
  • An alternative and sometimes better approach to install your dependencies is to use the "npm update" command.
  • This will install any missing packages and will update all of your existing dependencies to their specified version.
          npm update

e.g.        
          D:\Freelance Work\My Blog\Mean Introduction\NodeJs\Test> npm update

How to Update the package.json file ?

  • Another great feature of the npm install command is the ability to install a new package and save the package information as a dependency in your package.json file.
  • This can be accomplished using the --save optional flag when installing a specific package.
  • For example, to install the latest version of Express and save it as a dependency, you can issue the following command.
         npm install express --save

e.g.        
          D:\Freelance Work\My Blog\Mean Introduction\NodeJs\Test>  npm install express --save
  • In the preceding command will install the latest version of Express and will add the express package as a dependency to your package.json file. 
                        {
                             "name": "MEAN",
                             "version": "0.0.1",
                                   "dependencies": {
                                            "express": "^4.11.2",
                                       },
                        }

References

  • In this article, You learned how to install Node.js and usage of the Node.js CLI.
  • You learned about NPM and discovered how to use it to download and install Node.js packages.
  • You also learned how to easily manage your application dependencies using the package.json file.
  • In the next article,we'll discuss the basics of Express.js.
  • I hope this helps to You.Comments and feedback greatly appreciated.

MEAN Stack Development with MongoDB

Introduction

  • This is just an introduction of MEAN Stack and MongoDB.
  • I'll write more about MEAN Stack in the future.
  • This is the first part of the series of articles which I will write about the MEAN.

MEAN Stack Development with MongoDB

What is MEAN Stack ? 

  • The MEAN stack is a powerful, full-stack JavaScript solution.
  • That contains 4 major building blocks or components.
  • Which are MongoDB, Express, AngularJS and Node.js 


What is MongoDB ?

MEAN Stack Development with MongoDB
  • MongoDB is an open source, document-oriented database.
  • In MongoDB you can store JSON-like documents with dynamic schemas.

What is Express ?

MEAN Stack Development with MongoDB
  • Express is a minimal and flexible Node.js web application framework.
  • It provides a robust set of features for building single and multi-page, and hybrid web applications.


What is AngularJs ?

MEAN Stack Development with MongoDB
  • AngularJS is a web client framework.
  • It extends the HTML vocabulary for your application.
  • The resulting environment is extraordinarily expressive, readable, and quick to develop.


What is Node.js ?

MEAN Stack Development with MongoDB
  • Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications.
  • It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
  • It's perfect for data-intensive real-time applications that run across distributed devices.

What are the advantages of using MEAN stack ?

  • A single language is used throughout the application.Which is JavaScript.
  • All the parts of the application can support and often enforce the use of the MVC architecture.
  • Serialization and deserialization of data structures is no longer needed hence data marshalling is done using JSON objects.


How to Install MongoDB on Windows ?

Step 1 :
  • My laptop is having Win 8.1 OS.So I'm going to download the Windows 64 bit Msi version.
  • You can download it from Here.

MEAN Stack Development with MongoDB


Step 2 : 
  • Just double-click the Msi file.
  • Then you'll see a set of screens which will appear to guide you through the installation process.
  • I accepted default settings for all the screens.

MEAN Stack Development with MongoDB



Step 3 :
  • The default path for the installation as shown below.
MEAN Stack Development with MongoDB


Step 4 :
  • I have created a new folder named as 'mongodb' on 'c:\' drive.
  • This step is optional.You can run MongoDB from any folder as you wish.
  • After that contents have been moved into that folder as shown below. 

MEAN Stack Development with MongoDB









How to Run MongoDB ?

Step 1 :
  • MongoDB requires a 'data' directory to store all the data.
  • MongoDB’s default data directory path is 'c:\data\db'.
  • I have created  a 'data\db' folder on 'C' drive.It looks like below.

MEAN Stack Development with MongoDB

Step 2 :
  • Run 'Command Prompt' with “Administrative Privileges”.
  • To start MongoDB, run 'mongod.exe' on command prompt as shown below.
          C:\WINDOWS\system32>cd C:\mongodb\bin
          C:\mongodb\bin>mongod.exe

MEAN Stack Development with MongoDB











  • You can see that the main MongoDB service where it starts listening to the default port 27017.
  • Which means it starts the main MongoDB database process.
  • The 'waiting for connections' message in the console output indicates that the mongod.exe process is running successfully.


How to Create a Windows Service for MongoDB ?

  • We can use Windows Service for running MongoDB automatically after every reboot cycle.
  • Which is the more popular approach for running MongoDB on windows machines.
  • Before we begin setting up MongoDB as a Windows Service, it's considered good practice to specify a path for the MongoDB log and configuration files.Let's do it first.

Step 1 :
  • Create a directory inside the 'data' folder as 'log'.
  • This is for the log files.It's shown as below.

MEAN Stack Development with MongoDB

Step 2 :
  • Let's create a configuration file.
  • This configuration  file can include any of the configuration options for mongod.
  • But it must include a valid setting for the 'logpath' which we created on Step 1.
  • The following command creates a configuration file, specifying both the 'logpath' and the 'dbpath' settings in the configuration file.
         C:\>echo logpath=c:\data\log\mongod.log> "C:\mongodb\mongod.cfg"
         C:\>echo dbpath=c:\data\db>> "C:\mongodb\mongod.cfg"

MEAN Stack Development with MongoDB
  • It creates 'mongod.cfg' file inside the 'C:\mongodb' folder.
  • It looks like below.

MEAN Stack Development with MongoDB

Step 3 :
  • Let's create a MongoDB service.
  • For that,you have to run the below mentioned command on command prompt (which is having administrator privileges ( Run as administrator)).

C:\> sc.exe create MongoDB binPath=  "\"C:\mongodb\bin\mongod.exe\" --service --config=\"C:\mongodb\mongod.cfg\"" DisplayName= "MongoDB 2.6 Standard" start= "auto"

Note : sc.exe requires a space between “=” and the configuration values (eg “binPath= ”), and a “” to escape double quotes.
  • If the service was successfully created, you will get the following log message.
          [SC] CreateService SUCCESS

MEAN Stack Development with MongoDB


Step 4 : Run
  • We have installed our MongoDB service.Now you can run it by executing the following command in the command prompt window.
          C:\>  net start MongoDB

MEAN Stack Development with MongoDB


Step 5 : Stop
  • If you need to stop the MongoDB service, use the following command.
       C:\>  net stop MongoDB


MEAN Stack Development with MongoDB

How to use the MongoDB shell ?

  • You can find out the MongoDB shell inside the 'C:\mongodb\bin' folder. 
  • Which allows you to interact with MongoDB server instance using the command line.

Step 1 :
  • To start the shell, you have to navigate to the mongodb bin folder as shown below.
          C:\> cd C:\mongodb\bin


Step 2 :
  •  After that you can run the 'mongo' service as shown below.
          C:\mongodb\bin> mongo
  • If you successfully installed MongoDB, the shell will automatically connect to your local instance, using the 'test' database.
  • You will see a console output similar to the following screenshot.

MEAN Stack Development with MongoDB


Step 3 :
  • Let's do some MongoDB data manipulations.
  • Type following command on the MongoDB shell.
          > db.books.insert({title: "Hello MEAN World"})
  • The preceding command will create a new 'books' collection and insert a JSON object containing a 'title' property.
  • To retrieve the book object, execute the following command.
        > db.books.find()
  • The shell console will output the following message.
         { "_id" : ObjectId("54cc8cfedc0606c101f00e95"), "title" : "Hello MEAN World" }


MEAN Stack Development with MongoDB
  • It means your MongoDB instance is working properly and you have successfully managed to interact with it using the MongoDB shell. 
  • Congratulations !. You have done that.

Step 4 :
  • If you need to exit from the shell,just type 'exit'.
         > exit


What is Robomongo ?

  • Robomongo is a shell-centric cross-platform open source MongoDB management tool (i.e. Admin GUI).
  • Robomongo embeds the same JavaScript engine that powers MongoDB's mongo shell.
  • Everything you can write in mongo shell where you can write in Robomongo.


How to Install Robomongo on Windows ?


MEAN Stack Development with MongoDB

  • Just double click the downloaded installer set-up and accept the default settings for all the screens.That's it.

MEAN Stack Development with MongoDB


How to work with the Robomongo ?

Step 1 :
  • Just double click the auto created 'Robomongo' short cut on the desk top.
  • It'll open the GUI of the Robomongo.

Step 2 :
  • After that you have to create a connection string for the MongoDB database.
  • You can use 'Create' link to create a new database connection as shown below.

MEAN Stack Development with MongoDB

  • I have given a connection name as 'MongoDbTest' and Address with port number (i.e. port of MongoDB server) as 'localhost : 27017'.
  • You can test your connection by using 'Test' button as shown above.
  • After that you can use the 'Save' button to save the changes.

Step 3 : 
  • You can do huge set of MongoDB operations by using this awesome Robomongo GUI.
  • Just a few of them are shown below.

MEAN Stack Development with MongoDB

  • Here I have used the 'test' database with the 'books' collection where we created earlier.
  • If you need to see the full list of features, please visit their home page here. 


References


Conclusion

  • In this article, you learned basics of MEAN stack development.
  • You also learned how to install MongoDB and how to connect to your local database instance using the MongoDB shell.
  • You also learned how to install and use the MongoDB management tool Robomongo GUI.
  • In the next article,we'll discuss how to install Node.js and the basics of Node.js.
  • I hope this helps to You.Comments and feedback greatly appreciated.