Showing posts with label express. Show all posts
Showing posts with label express. Show all posts

Thursday, 28 August 2014

Creating A Simple RESTful Web Service using Express Node.js - JSON Data for Different Platforms (Part - 1/2)

As Every Day Device Shrinks and the way how it is connected to the Server to pull data and Append data becomes complex.Here is the solution for that because usually Network related application are data driven applications,where data is the king and we could make up something as we need .JSON data - Javascript Object Notified which is considered to be Light weight data for data sharing between two platforms or systems.where XML is also used,but JSON is preferable for mobile applications to pull data easily via AJAX JQuery with cross origin request.API is common term where in web apps are data driven application,Data is used to build apps which paves a way to create Application Programs with those data.


Creating A Simple RESTful Web Service using Express Node.js - JSON Data for Different Platforms (Part - 1/2)
Creating A Simple RESTful Web Service using Express Node.js - JSON Data for Different Platforms (Part - 1/2)
Download :  Download Demo

The Main Aim of Creating such Rest API Web Service is to share the data between application where we could create apps that can reach a huge peoples!


There are lot of standards in REST API building to prevent attacks,scaling and illegal requests.To prevent that we could add APP ID and Secret token basically (BASIC).Oauth2 standards would have such process



  • The Specific Application is registered with APP id and Secret key and may be where the origin request is arises and handled.
  • Provide appId and Secret Key for Application.where the app developer just pass them via Header
  • [may be sometimes they have permissions if it is a Big Company like Facebook,Google,Twitter
  • And Action to be perform is passed via Body part with data to be processed.
  • Return Success/Error messages with Data to application in JSON/XML format
The above steps are basic simple but there are lot of methods to be followed for full Fledged API platform to run with performance.

Scope:

My Application will pull data from Server which is managed by Node.js as Backend Server Responding the Requests!

Warning:

  • This is just Basic Model Of pulling data from server to display at cross platform application.
  • I haven't enabled any HMAC Security and any other securing features to authenticate.
  • I am using Express Framework 4.x.x version since it is handy for me.
  • I am versioning the API via URL for my easiness.There are lot of controversies in this versioning API via URL/or body part!

Node.js Express Backend:


First of All let me Create a Simple routes that can respond with JSON format data and accepting inputs via body and headers.

package.json

{
"name":"SampleRestAPI",
"description":"Basic Web Service",
"version":"0.0.1",
"private":true,
"dependencies": {
              "express": "~4.x.x",
               "body-parser": "*",
              "mongoose": "~3.8.11"
  }
}

server.js:


Important part is to create routes for your application and which can determine to give away resource! Rest API is meant for that! Asking for resource via HTTP methods. :)
simple examples of creating routes for ur application:


app.get("/",function(req,res)
{
      //get movies list data
});
app.post("/year",function(req,res)
{
        //filter year wise result movie
});
app.post("/create",function(req,res)
{
      //create new movie in Database
});
app.put("/update",function(req,res)
{
     //update certain part of the object(movie)
}
app.delete("/delete",function(req,res)
{
     //delete object(movie)
}



So,from the above Routes created , you may get some idea to build yours,there are lot of techniques in routing in express,if you can manage it apply and make it,others simply you can use and further learn more about creating complex application routes[Express 3.x.x] version
And Finally respond your client with the JSON data appropriately using res.json method

Creating Database for Back End Data Handling With MODULUS.io:


Since we are going to handle a considerable amount of Data lets have simple schema holding recent film,2014 films and love.horror movies in single schema document with sub-document.


Create A account in modulus.io for free Mongoose Database hosting.It is actually free but we wont use it upto such extent! :)



  • Create An account with your email account
  • Create New -> Database service
  • Select preferred Hosting such as Amazon/Joyent
  • and give Database Name , here Database Username/Password is very important.
  • After creating they will provide a URL to connect to your database

mongodb://<username>:<password>@proximus.modulusmongo.net:27017/zzzzzzz


The Above GIven URL is for example ,you may get different Domain ,Port number and Database unique ID


database.js :



Sample DB from modulus.io
This is created for connecting your schema to Hosted DB and transferring of Data from application to Database and reverse to :)


 module.exports = {'url' : 'mongodb://<username>:<password>@proximus.modulusmongo.net:27017/zzzzzz' };

server.js Additional to Routes :

Let me explain the logic of Database query here,Btw we connected to mongoose db with url connect() using the url.



  1. GET /  -  This route is used to request whole movie database with native function find() without passing any params and return type to client as JSON Format
  2. POST /create - this route is used to create new movie object and append to database and return user with unique id for the insert
  3. PUT /update - this route is used to update a movie by passing _id and necessary details to update.here findbyIdandupdate function is used to find id and update simultaneously.
  4. DELETE /delete - this route is used to delete a movie by passing a id and remove function is used to delete from our database
  5. POST /year - this route is used to search for particular year movie details using find() function and return as JSON[this can be done using GET method by passing params in url]



model.js:

var mongoose = require('mongoose');
var movie_api = mongoose.Schema({
    movie          : {
        name       : String,
        year         : String,
        director   : String,
        genre       : String,
        thumb      : String ,
        music       : String,
        rating       : Number ,
    }

});

//you can have methods here 
module.exports = mongoose.model('movie', movie_api);


Thus in Above model.js we have defined our database schema and how we are going to organize our data in mongoose database.



My Try over API from REST CLIENT :

Download the REST API Client from here. abd have a try.


  • Download the files and extract
  • execute npm install
  • run server.js file
  • try the API URL from client addons
  • report for errors/bugs s.shivasurya@gmail.com 

REST ADVANCED CLIENT CHROME ADDON - A try over my API


Note : I haven't validated any data from user and retrieve from DB.This is basic app for RESTful Webservice and not suitable for production mode.

Try the Demo files in your localhost/node.js hosting.if any error/suggestion/bug do comments below or mail me s.shivasurya@gmail.com or connect with me in FB/Twitter for help and other sorts.Share is care

Wednesday, 13 August 2014

Deploy Node.js Application on Openshift Hybrid Cloud - i-visionblog

Nowadays Developers/End users prefer cloud services for their Usage,Storage and Demanding service in one clicks either using credit cards for paying the usage.Business start ups / Developers prefer to host their applications on cloud public for users access, and to manage a huge data ,analyse by powerful cloud machines provided by cloud service providers.Where Business peoples/Developers and others can forget their Hardware specifications and start involving in the Application designs and management.Cloud plays important part in business side data/analytics handling!


Deploy Node.js Application on Openshift Hybrid Cloud - i-visionblog

Article About Cloud at i-visionblog : 

  1. What is Cloud Computing ?  - A Good Explanation for Easy Understanding with Video.
  2. Deploy PHP application - Heroku Cloud [ free untill you add add-ons]
  3. Deploy PHP application - +IBM Bluemix Cloud [free trial Period ]
  4. Deploy Node.js Application - +IBM Bluemix Cloud Foundry
  5. Deploy Mobile Application DataStore online - Parse.com [ Free for Personal apps ]

Scope : 

Here You can develop a simple Node.js Application at Localhost and make it as deployable at Openshift(Red Hat) .

Installation : (Windows Machine ) :

  • Download GIT command line and make sure accessible via command Line [ git --version  must output with version number] click here to download.
  • Download Ruby Installer [ Stable version ] from here Must be accessible via command line.
  • once all goes well do  C:\> gem install rhc  Must Install you with RHC Client for windows.
  • Go to Red Hat Openshift Website and create a Account & then  Create a Domain name.  
This is Enough to Deploy your app in minutes from desktop.however Mac,Linux others can follow deploy process which is same since Git is Standard.Installation can differ.

RHC Setup :

 C:\ >  rhc setup 

It may prompt you for Adding server host ! Just Skip it by Pressing Enter key.you can add it later too.
Then It may ask you to login with openshift account online emailID and Password in CMD if you're doing it first time.
And allow to add a Private SSH file for authentication purpose - RSA public key adding it permanently on you command line for 30 days life time.Enter "yes" to accept and proceed.

Creating Application :

 I would Generally appreciate you to Go to Openshift website to have Overview of application present.
There are lot of application present to be installed.
  • Add Node.js Application by clicking add application
  • Name your application [ Note that you must have added Domain Name already after sign up ]
  • And Add a custom Url for your application - [ can be changed if you have DNS records ]
  • Git repo is optional here 
  • If you want to scale application for more users,Do load balancing here- [note if you have database in application make sure it is shared across application on different instance shares it]
  • Create Application and Get the Source url starting with SSH:// on console application page
Adding Source :
adding source to application !
  • C:\ > git clone [copy the SSH:// URL here ]
  • This would initialize your application directory for Deploying.
  • Copy your Application Folders/Files with package.json and server.js [server.js name is mandatory to start your application ]
Changes in server.js:

Your Localhost is not equivalent to Cloud Setup so, you must provide ip and Port variable to run your application on cloud.And make sure port,ip should be same as Below for any type of Node.js 

var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var ip = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";

and At last while making the application to listen to given port,ip

server.listen(port,ip);

A sample of Server.js file :


Thats All just move in to your Directory through Command Prompt .execute three commands succesive manner and deploy your application.For updating application just follow these three commands one by one.

  • C:\ > git add .
  • C:\> git commit -am "initial commit"
  • C:\> git push origin master 
Debug & Logs :

  • just type  C:\> rhc tail <app_name>
will provide you ongoing Requests / Errors in the Node Applications .

Thus we have Successfully Deployed First Node.js Application on Openshift Hybrid Cloud.Openshift is Free to have Three application with Scalable Application , Https Connection access and also more add-ons With Limited three Gears each Application & 1 GB space.Have a try ! be cautious before adding Credit cards because it may cost you for overusage!.

Report for Error/Suggestions as Comments or mail me s.shivasurya@gmail.com / or connect with me in Facebook/Twitter.Enjoyed the Posts,Share is care and Do comments!

Thursday, 7 August 2014

Handling File Uploads and Downloads In Node.js with Ajax - Express Framework

Uploading the files is important aspect of transferring the data to the server from the client and moving it to the storage to share for downloading and later usage also.However encryption is important aspect of saving the Files safely with pass phrase encryption/and Decryption on Downloading or requesting.These types of Application is done generally in Cloud for user safety for files from Illegal Access.However Node.js doesn't allows you to peep into directories and get download the files where these directories are generally private unless you make it as Public Folder.This is a Main Advantage for us.



Resource : Download Demo files 

Scope:
We are Just going to Upload the Files via Ajax and Retrieve Files from Directory using FS module and make to available for Download.This Application is simple integration of Storing files online and retrieving it from anywhere.


Package.json:

Create package.json and run the installation to create dependencies for our app.
{
  "name": "expressapp",
  "version": "0.0.1",
  "description": "uploading files-Downloading",
  "dependencies": {
    "express": "3.x.x",
    "Jade" : "*"
  }
}

server.js:

Let me Explain about the server.js - i am initializing the app with Express Framework.Then setting the port for the app to listen in my Configuration function by app object of express.For Templating language i have used JADE to parse the variables across the files(view) and set up my View directory.
Then i have initalized with body parser for fetching my posted values via body and setting the limit of upload and keepExtensions of my uploaded file and setting the path for my directory for uploaded files in Server.And i have set up a public static folder for Javascript/CSS files.

So, creating the routes explicitly read here and here to create route url for Node.js Application.I have created four routing URL for index,Upload,Download,listing files.


Replace the Path URL to fetch corresponding files from your directory.For the Convenience I have used JADE  as Templating language ,You must use it carefully since it is space/tab sensitive.Use online Jade input/output for verfiying your syntax.

FileSystem Function :



var fs = require('fs');
fs.readdir('C:\\Users\\S.Shivasurya\\Documents\\node\\expressapp\\tmp\\', function (err, files) {  if (err)
    throw err;
var result=new Array();
 for (var index in files) {
  if(fs.lstatSync("C:\\Users\\S.Shivasurya\\Documents\\node\\expressapp\\tmp\\"+files[index]).isFile())
    result[index]=files[index];
 }
 res.render("files",{ result:result });  //rendering file.jade for viewing
 });

The above functions just read the given directory and fetch all files and save it in an array if it is file and not a directory! refer to node.js docs for more fs functions

Script.js (Ajax file Upload):


A Third Party plugin for AJAX upload with progress bar:



Jade for looping Array :

file.jade to list out the Files in directory while accessing /file route


extends layout
title files
block content
  div.container
    div.content   
        ul
          - each results in result
            li= results
            input(type='submit', value='Download',id=results,class='download')

  div.footer

Index.jade:


extends layout

block content

  div.container
    div.row
      div.span12
        form(method='post', action='/')
          legend express-upload-progress
          input(type='file', name='myFile', id='myFile')
          p
          div.form-actions
            input.btn.btn-primary(type='submit', value='Submit')
    hr
    div.row
      div.span12
        div.progress.progress-striped.active.hide
          div.bar(style='width: 0%')
    div.row
      div.span12
        div.alert.hide
          button.close(type='button', data-dismiss='alert') x
          span
            strong.message

Layout.jade:


doctype html
html
  head
    title express-upload-progress
    link(rel='stylesheet', href='/stylesheets/bootstrap.min.css')
    link(rel='stylesheet', href='/stylesheets/style.css')
  body
    block content
    script(src='/javascripts/jquery-1.9.1.min.js')
    script(src='/javascripts/bootstrap.min.js')
    script(src='/javascripts/scripts.js')

Download script as client javascript :

This is a simple script that can post the filename we request and download the file from Node.js express routes /download.i am not doing any Ajax operation here.just submiting POST request to server for downloading a file.

on clicking the download button,Jquery request for the file and downloads on the browser automatically.


$(document).ready(function(){
  $(".download").click(function(e){
   var filedata=this.id;
  $('<form action="/download" method="POST">' + 
    '<input type="hidden" name="file" value="' + filedata+ '">' +
    '</form>').submit();

    });
});

I Have tested on Localhost node.js server ,Have a try with demo files provided,If any error/Bug in Code/Article post as comment/mail me s.shivasurya@gmail.com.connect with me via Facebook/Twitter for help.Share is care.report for bugs.

Wednesday, 2 July 2014

Node.js With Express.js Framework - Routing Techniques With Name-space

Express.js Framework on Node.js doesn't inherit from Connect router instead it is built with it.Routing is useful to create Friendly URL's and manage your stuffs in site in elegant manner.When your application becomes complex for managing routes.here comes Namespaced routing for managing urls with namespaces However only you must select the routes accordingly for your application.Express.js doesnt force you to use particular routing.As like as An unplanned routing system for CMS will suffer a huge during the production environment.so,choose and manage your routes efficiently.

This Post is for 3.x.x version of Express.And note that this is an inbuilt feature in 4.x.x version of express.i will update how to work with 4.x.x routing techniques.

express-js-i-visionblog













Downloads :   |    

Namespace Express routes :

When your application becomes complex you have to manage a big set of uri in your application,however taking it to seperate routes handlers is second part.let us see how could we segregate them as per needs.when you write CMS,Store or anything you need to club the URI for certain functions.Here Namespace comes to recover us.

Expected Namespace URI structure :
when i plan for app store as well as books store within my single website that would be tedious for me to manage them.so i may need URI like below.

  
   surya.com/apps  --> index page of apps store
   surya.com/apps/facebook -->facebook app to download and description
   surya.com/apps/googledocs --> googledocs app to download and description

   surya.com/books -->index page of book store
   surya.com/books/headfirstandroid ->headerfirst android book for download.
   surya.com/books/edit --> must load details for admin to edit the items on index page.


Installation (package.json):


{
"name": "routersample",
"description": "simple routing and namespaced routing",
"version": "0.0.3",
"private": true,
"dependencies":
{
"express": "3.x",
"express-namespace": "*"
}
} 

Run your npm install command in your application directory.Learn more here

Defining Routes :

Note Here we are initializing the http and express before namespace and then app is initialized after namespaced.unless your app with namespace wont work for your needs.

var http = require("http");
var express = require("express");
var namespace = require("express-namespace");
var app = express();


Defining routes as per our needs goes below :



  app.use(app.router);
   app.namespace('/apps', function() {
       app.get('/', function(req, res) {
         res.send('Respond Client with Apps Index Page here');
        });

        app.get('/facebook', function(req, res) {
           res.send('Show Facebook App with download link and description');
        });

       app.get('/googledocs', function(req, res) {
           res.send('Show Google Docs App for download link and description');
        });
       app.delete('/delete/:apps', function(req, res) {
           res.send('delete application with  requested ' + req.params.apps);
        });
 });

 app.namespace('/books', function() {

    app.get('/', function(req, res) {
       res.send('Respond The Client With Books Index Page');
    });

    app.get('/headfirstandroid', function(req, res) {
       res.send('show Head First Android book To the User with Relevant Information');
    });
   
 app.get('/harrypotter', function(req, res) {
        res.send('show Harry Potter Book To the User with Relevant Information');
    });
 });
http.createServer(app).listen(3000);


Namespace can be nested with one another too.and note that app.router must be loaded explicitly before starting namespace routes.And initialize the namespace routes before the app gets initialize.
Now point your browser url respective to your routes it works beautifully.This is why we call express as bloat free framework


basic routing (video) :

basic form submission and routing video.download files are available above. If you can find any bugs/Errors in my Demo or above code just disqus with me in comments or mail me s.shivasurya@gmail.com.share is care.

Wednesday, 18 June 2014

Node.js With Express Framework Basic App on IBM Bluemix - Deploying Cloud Foundry

As i got request from +Yogesh Asthana brother by Google+ on Node.js app basics and other frameworks to build Highly scalable application that can be real time usage.Node approaches different manner in responding requests and while other scripting language make threads and use the RAM and core with full fledge usage and blocking approach.But Nodejs take care since it is written as Asynchronous functions with non blocking I/O behavior ,where when u Do some task in Nodejs like Database operations or file upload ,which is cost effective, network based and time consuming operations are made asynchronous where it will return the results later.here the next line code executes without any blocking.Thats why it is faster and may be due to other reasons to.This is built based on Google Chrome Run Time Javascript V8 compiler,by Google Engineers.
Node.js With Express Framework Basic App on IBM Bluemix - Deploying Cloud Foundry
nodejs with express i-visionblog

Demo | Download | Contact 

Scope : 

By this Post You could successfully Run a simple app Nodejs with Express in Blumix Cloud,IBM.
I have explained to build a simple app in my Youtube Video Channel.Any way i will Provide Explanation also by words below.

Downloads :

Visit Nodejs website and based on the operating system install the Nodejs in Your local machine for Locally testing your app before deploying cloud.

As my Last post gave You idea of IBM Bluemix,Just Create account in Bluemix and install Cloud Foundry Tool. - refer Here to install

Let Us Code : 

Basic Nodejs App contains Package.json file through which you can download all the dependencies and maintain your app efficiently even if you migrate from System.simply by using npm command you can fetch and install the dependency modules.You can simply initialize your package.json By command line

npm init  // will ask you one by one and you can provide details corresponding.

here i am explaining a simple app named sample and with description and others too.
package.json 

{
"name":"sample",
"description":"second app with node js",
"version":"0.0.1",
"private":true,
"dependencies":
{
"express":"3.x"
}
}

For Further Create a directory and do all the above operations.
now create app.js file to regulate and control our app.here app.js name is optional you can name it as per your needs.[main.js is mostly recommended]

app.js : 



var http = require("http");
var express= require("express");
var app= express(); // here we are defining our app and initializing with express 

//i am going to make basic routes for app

app.get("/",function(req,res){
res.send("<h1>this page is from Node js Express APP - Shivasurya - </h1>");
});

app.get("/welcome",function(req,res)
{
res.send("This welcome page ");
});


app.get("/about",function(req,res)
{
res.send("This is from About page ");
});

app.get("/example",function(req,res)
{
res.sendfile('example.html');
//the above fun send html file to browser so that browsers can intrepret
});


//similarly you could add routes and send the text as html or even html files to the browser using res.sendfile();


http.createServer(app).listen(3000); //here 3000 is port number for app



//thus we have created a simple web app using Express framework

In the Above Code we created Express object of response and request and we accessed send() and sendfile() functions.there are still more functions can be invoked,by next post i will brief it about the Routing techniques and best practices.Open Your browser point to localhost:3000 and check the routes we defined already in app.js

Time To Deploy On Cloud :


Check that You have Installed by Using the Command in Your Command prompt.

C:\>cf -v  

must output the version number of your CF Tool installed currently.Move to Your Current Directory where your project lies.We need Small Changes in Our code since it is Production Environment however your going to test in on Cloud as Sand Box mode.

Code Changes For deploying at Cloud :

thus we had Simply added VCAP service Port Number and Host to work perfectly on cloud environment.

var port = (process.env.VCAP_APP_PORT || 8192);
var host = (process.env.VCAP_APP_HOST || 'localhost');
var http = require("http");
var express= require("express");
var app= express(); 


app.get("/",function(req,res){
res.send("<h1>this page is from Node js Express APP - Shivasurya - </h1>");
});

app.get("/welcome",function(req,res)
{
res.send("This welcome page ");
});


app.get("/about",function(req,res)
{
res.send("This is from About page ");
});

app.get("/example",function(req,res)
{
res.sendfile('example.html');

});


http.createServer(app).listen(port,host); 



now create a Manifest.yml file to instruct Bluemix to allocate Nodejs Applications,space Instance and others.i have explained in my Last Post on Creation and Usage of Manifest.yml.

Manifest.yml


---
applications:
- name: sampleapp1995
  memory: 256M
  instances: 1 
  host: sampleapp1995shiva
  command:node app.js

Just Push the App using the command below in Command prompt

$ cf push
Progress :


finally after deploying
during installation depedencies automatically screenshot


note: Work with caution because cloud may costs you money for over usages and scalling instance.And you can use HTTPS connections too.

I have deployed my application on Bluemix.just have a try since it is free till june month 2014.just share your experience with comments and post errors.Contact with me in s.shivasurya@gmail.com.Share is care.