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 : https://nodejs.org/download/
2. Install Nodemon
npm install -g nodemon
3. Install Mongo DB
Win : http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/
Max : http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/
Linux : http://docs.mongodb.org/manual/administration/install-on-linux/
4. Created a Git Repo for the same.
After node is installed.
Tools Of Trade
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
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 : https://www.npmjs.com/package/express
Mongoose : https://www.npmjs.com/package/mongoose
body-parser : https://www.npmjs.com/package/body-parser
node-restful : https://www.npmjs.com/package/node-restful
Commands are as follows
npm install –save express
npm install –save mongoose
npm install –save body-parser
npm install –save node-restful
! email@example.com 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.
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.
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”
Thats All, Hope you all enjoyed 🙂