Hot Proton

1 Comment

Node Express MongoDB based REST Services in few minutes

Node Express MongoDB based REST Services in few minutes

This is as simple as its gonna get to create a fast clean REST based API in under few minutes.

1. Install Node :

2. Install Nodemon
npm install -g nodemon

3. Install Mongo DB
Win :
Max :
Linux :

4. Created a Git Repo for the same.

After node is installed.

Tools Of Trade
1. cmd.exe
2. Git Bash
3. Visual Studio Code / Sublime Text / Any code editor of choice
4. Robo Mongo : my GUI of choice for MongoDB on Windows

With the basic setup done lets get started.

. Fire up your command prompt and let the party begin.
. Created a Git Repo at GitHub so everyone can access the code.

. Fire up Git Bash on your directory that you want to map to this Git Repo

. Git Clone to link your Git Repo with your folder

. Open command prompt on the same directory, lets start with building a node app.

Type the following commands on the command line

npm init

This will create your package.json file with the Node App Configuration

. Install NPM Packages that will help us create our Node REST App
Express :
Mongoose :
body-parser :
node-restful :

Commands are as follows

npm install –save express
npm install –save mongoose
npm install –save body-parser
npm install –save node-restful

! node-restful@0.1.18 wants mongoose@~3 this is the error thats received when you install “node-restful” but seems to be working fine with latest mongoose.

As you can see we are all set to now start coding our new Node App
The depenendencies have been downloaded.

!For those coming from .NET world : NPM is like Nuget for Node

. Create a the following files and folder structure

“server.js” will be our starting point
“models\product.js” will be our product model
“routes\api.js” will be out configuration for api routes


This is the start file and we have just added ref. for our dependencies

mongoose : connects to our mongodb database “NodeMongo” which is running on
localhost and it has the the products collection.
Note : we are not going to create the MongoDB collection called “products”
the code will automatically do it for us.

CORS : Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources (e.g. fonts, JavaScript, etc.) on a web page to be requested from another domain outside the domain from which the resource originated.

Out REST API will be running on one domain and the requests originate from another domain. So in order to allow the other domain requests to talk to out rest service we need to perform this step.


This code defines our Product Model


This code defines the various Routes for enabling GET PUT POST DELETE operations.
Thats it just a few lines of code and we have our REST Routes done.

. Lets run our code.
node server.js
nodemon server.js

nodemon will keep watching your code for changes and you do not need to
start and stop server again as with “node server.js command”

Now that our code is up lets do a GET Request

. Using Postman to check GET POST PUT DELETE, its a Chrome extension to test rest based services





. The data in MongoDB using Robo Mongo

. Lets Add , Commit and Push to Git Hub
git add -A
git commit -m “Initial Commit”
git push


Thats All, Hope you all enjoyed 🙂