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.

Pre-requisites
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.
b- https://github.com/sanketss84/MEN-Rest

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.
https://github.com/sanketss84/MEN-Rest.git

. 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 : 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

! 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

Server.js

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.

models\product.js

This code defines our Product Model

router\api.js

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
OR
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
http://localhost:3000/api/products

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

GET

POST

PUT

DELETE


. 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 🙂

Advertisements


Leave a comment

Lets do some magic with Git Bash : Visual Studio Code : Sublime Text

Git Bash : Visual Studio Code : Sublime Text with Git + Git Gutter Plugins

Wanted to check out how well is the Git integration with various code editors.

1. Installed Git Bash , Visual Studio Code, Sublime Text 3 with Git Gutter.
2. Create a Github Repo called test
3. Created a folder “C:\Sanket\Projects\GitHub” fired up Git Bash and did a clone of my “test” repo.
#git clone https://github.com/sanketss84/test.git

4. Now time to test the Git integration with both code editors
5. open your git test folders in both code editors

For Visual Studio Code
code .

For Sublime Text
subl .

6. Now we have both editors ready for comparison

7. Lets make some changes in Sublime and VS Code and see how its reflected

VS Code gives us that familiar green stripe where ever changes are observed
Sublime Text with Git Gutter gives us those small + symbols.
Pretty useful right to track code changes.

These changes are tracked live from Git Hub.

8. On saving the file in VS Code the Git Icon lights up so that we can commit the file to Github. Lets peform a commit and push this file to Github.





Done

9. Lets check out GitHub Page to see if the changes are reflected.

And this looks great.

10. Lets check it with sublime text.





 

Thats it hope you enjoyed it 🙂

Thats some really cool Git Magic.

 


Leave a comment

My Git Cheat Sheet

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Download Git Bash Here : http://git-scm.com/downloads
ReadMore : http://git-scm.com/doc
Git has various clients for various OS check here : http://git-scm.com/downloads/guis

// Once Git Bash is installed then configure this initially
git config –global user.name “Sanket Sonavane”
git config –global user.email “sanket@mail.com”

// Lets get started
git init projectname
then
cd projectname
create filename.ext
git add filename.ext
git commit -m “My first commit”

git clone urlname
git status

ADD > COMMIT > PUSH // will send the files to GitHub
git add filename.ext
git add . //All files in directory
git add -A
git commit -m “Message for commit”
git push

git pull // pull latest code from github

git log

// Branching and Merging
git branch // Lists branches
git branch branch-name // Creates a new branch of the current branch that you are on
git checkout branch-name // Used to switch to branch-name it can be any branch
//make your changes in the new branch and now we need to checkin and merge to main
then
git checkout master // Switch back to master
git pull //To check if there were any changes in master branch
git checkout branch-name // Switch back to your branch
git merge master // Merger current branch to master
git add . OR got add -A
git commit
//NOTE: When you see long commit messages hit “esc” :wq

 

Sublime Text Users check out Git Gutter and Git Packages they are really handy.