Wednesday, March 28, 2012

Power of Script writing in the world of JavaScript.

When i was intern, i got a change to choose the technology on which i could develop a tool. PHP or Java. I choose Java, why.... hmmm... its easy to learn, use, understand, scalable (a myth), etc, etc, etc.
Later understood facebook uses php a lot.

Browsed few perl scripts few years back and did not even like it.
Later understood perl developed is easiest in many ways and faster. Faster in execution too.

JavaScript........ahhhh, what is this, i did not like it. Some kind of client side work, which looks like always messy. :-(
Later understood that its not only the client side script any more.

So suddenly my thinking about the script writing, understanding, acceptance has changed. Script programming is amazing.

First got to know about MONGODB. Oh its amazing, it understands java script. We can do all crud
operations with java script syntax. Its a document oriented storage with "NOSQL".

Than checked which mapper can be used and found node.js -> mongoose.


 And tried some hands on with node.js + mongodb + express + mongoose + jade (a template engine). Jade for view, remaining things for model and controller.

 And commonality in all these things are java script is everywhere. Commonality, an important factor for a software product with quality. Of-course there are many point and which i do not want to touch now.
So the point is: 
Server side java script. 
Client side java script. 
Database operations in java script. 
Oh, its heaven --> IT IS THE WORLD OF JAVA SCRIPT.

I am going to take you through some steps which can be useful to start with server side java script. You can download the project from my git repository at


 https://github.com/deepaksinghvi/nodejssample

1) Download Install node from http://nodejs.org/. This installs node and npm ( node package manager).
    npm is used to install all the dependent modules, like mongoose, express, jade, stylus, etc.
2) Next install all the dependent modules:
    cmd prompt> npm install mongoose express jade stylus
      or you can do one by one also.
3) Now lets write few java script files
     a) load all the required libraries which we have just installed and create an application server.


     b) create model and controller scripts.

         Creating a model (models/model.js).
The controller part normally known in the node.js development worlds as routing, i have kept in the main script file only ( which is app.js).
Routing of creation of a new user. This creates a new user and saved it into the database.

User clicks on "Create User" button and it request gets routed to above mentioned script.

Once user gets added than listing of users shown from the table ( known as collection in the world of mongodb) or it can be shown if user clicks on "Show Users" link in the above image.


4) Enjoy the sample application.
    To start the application:
    a) Start database
         mongodb/bin>mongod --dbpath /data/db 
       
    b) Start sample application
         nodejssample> node app.js


I tried to provide a very simple sample to start with node.js and mongodb. Its a perfect match for a rapid web based development. 
WebSocket also looks very promising, a lot has been written about it. I would like to try some of the exercise on that and will be sharing that as well in the next blog (hopefully).



Heroku Custom Trust Store for SSL Handshake

  Working with Heroku for deploying apps (java, nodejs, etc..) is made very easy but while integrating one of the service ho...