Technology Powered Knowledge Base
Click Here To Make FREE Audio & Video Call From Web Browser!

Install and Working With NodeJS on Windows PC and OpenShift

VN:F [1.9.22_1171]
Rating: +13 (from 17 votes)
VN:F [1.9.22_1171]
Rating: 7.7/10 (30 votes cast)

Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it light weight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
 
We can write Server Side and Client Side application in same language as famous Java Script is enough for that.
 
NodeJS can run own HTTP server with any custom port and can directly communicate with client application with Server Host and Port. So that NodeJS can create custom Socket and can communicate with client app using the Socket.
 

Create Environment for Local Windows PC:
Download NodeJS for Windows in your local computer from http://NodeJS.org/. Be sure about OS architecture X86 or 64 bit and download the appropriate one. Now install NodeJS on the system and don’t forget to restart the system.
 

Run Custom NodeJS Code
Now create a java Script file with any name I can prefer server.js and write down following code into that:


#!/bin/env node
//  Include NodJS HTTP class for server creation
var http = require('http');

// Define Local Server or OpenShift host and Port
var ipadd = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 1234;

// Now this is the actual Server creation method
http.createServer(function (req, res) {
	res.writeHead(200, {'Content-Type': 'text/plain'});
	res.write("Welcome to NodeJS HTTP Server!\n\n");
	res.end("Server Running at http://" + ipadd + ":" + port + "\n\n");
}).listen(port, ipadd, function(){
	console.log("Server Running at http://" + ipadd + ":" + port);
});

Now place the file anywhere in your hard drive and open command prompt. Move to the location where you have placed the Java Script file and type command $ node that’s it. You should see a console message like ‘Server running at http://127.0.0.1:1234’ .
Your server is up and in working stage. Now open any web browser and type http://127.0.0.1:1234 you should see something like
‘Welcome to NodeJS HTTP Server!
Server running at http://127.0.0.1:1234’
 

This is your NodeJS HTTP server which is running on your local PC. Soon I will post more interactive and working script on NodeJS.
 

Start Working on RedHat OpenShift Cloud:
OpenShift is a free (Paid version also there) cloud platform which can support PHP, .NET, NodeJS, MySQL, Postgres, MongoDB etc several development environments.
 

To learn more about OpenShift Platform please take some note about following things,
1) Gear: Gear is virtual server block where your application can run. When you create an application OpenShift will allocate a gear where that application will run.
2) Cartidges: When you create your application, you also select from your choice of programming language, database, or other services via OpenShift cartridges. This can include your own favorite technologies via the custom cartridge capability.
3) Scaling: his is a loadbalncing technique which can automatically handle by OpenShift. But for your test application don’t use Scaling. If you allow your application to scale, OpenShift will set up a load balancer and allocate more gears to handle traffic as you need it.
 

So now create your free account at https://www.OpenShift.com or login with your http://www.RedHat.com account and create your application with NodeJS cartridge. It will automatically allocate gear for you with a public web url for access your application. You can create your application from RedHat client as well, as described in below stage.
 

Now we have to understand the code managing mechanism and operating the OpenShift Platform. We don’t have any choice to use FTP or Browser based code management here. We can use SSH based connection with OpenShift platform to our local computer. OpenSift is using GIT to manage and migrate codes on their environment.
 

Now we have to install RedHat Client Tool in our local machine. RedHat Console is developed in Ruby and we have option to install as Gem. So now install Ruby from http://rubyinstaller.org. Make sure to check the option “Add Ruby executable to your PATH”. Now install RedHat Client Tool as $ gem install rhc you may update the tool using command $ gem update rhc. Now you should run $ rhc setup command to configure OpenShift Client Tool with your OpenShift account. You should create a public SSH key and update the key in your account to communicate with local SSH and Git.
 

Setup GIT with OpenShift
Now your step is install Git on your computer. Git will help you to manage code on OpenShift Server. You can use any IDE to manage your code locally. Go to http://msysgit.github.io and download your git client and install it.
Create a directory on your local machine. Now do the Git Clone using $ git clone or from GUI tool. You can get your git url from your OpenShift configuration settings.
So now Opensift code is in your local machine you can add or modify existing or whatever you can create and Commit them into OpenShift Server. You can see a sample code for NodeJS also in cloned code which is basically created by OpenShift as sample NodeJS code.
After an update to your code you should run Commit and use Push to take effect on live server including deploy to the server.
 

Setup Shell with OpenShift
Now you have to access the server with SSH. We know putty is most famous application in windows for uses unix like environment and access the shell. When rhc tools are installed, your SSH keys are created in the OpenSSH format. This format differs slightly from the PuTTY format. In this step, we’ll import the OpenSSH key that you have created earlier and convert it to native PuTTY format.
 

Go to http://www.putty.org and download putty for SSH access and to solve putty private key error download PuTTyGen.
 

Start PuTTYgen and select Conversions -> Import Key and select id_rsa you can get the file in windows environment as drive:\Documents and Settings\user\.ssh folder on WindowsXP and drive:\Users\user\.ssh folder on Windows 7
 

Once imported, you will see the key data information in Puttygen. Then, click on Save private key and choose the same location of .ssh folder. The file name defaults to id_rsa.ppk.
 

You should now have three files in this folder: OpenSSH private key id_rsa, PuTTY private key id_rsa.ppk and public key id_rsa.pub.
 

You can get your SSH access details from your OpenShift configuration settings. Start PuTTY and paste Gear name or host name what you had in SSH access. Then, navigate to Connection->Data in the left panel and paste the complete UUID as the Auto-login username, as shown below. Since the UUID is quite long, it may not fully display. Lastly, navigate to Connection->SSH->Auth and select the id_rsa.ppk as your Private Key file for authentication. Now save the settings for feature use and click on open and you should see a welcome message. So you are ready with your application’s shell.
 

Now you can run tail_all command in shell prompt to display application log entries. You can run following commands to manage your app ctl_app start ctl_app stop ctl_app restart and ctl_app status.
If you like to see NodeJS and Node Package Manager’s version you should run node –version and npm –version command. You know npm command is required to install other NodeJS plugins e.g. socket.io or peer.js.
 

Run Custom NodeJS Code
Now create a java Script file with any name I can prefer server.js and write down following code into that:


#!/bin/env node
//  Include NodJS HTTP class for server creation
var http = require('http');

// Define Local Server or OpenShift host and Port
var ipadd = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 1234;

// Now this is the actual Server creation method
http.createServer(function (req, res) {
	res.writeHead(200, {'Content-Type': 'text/plain'});
	res.write("Welcome to NodeJS HTTP Server!\n\n");
	res.end("Server Running at http://" + ipadd + ":" + port + "\n\n");
}).listen(port, ipadd, function(){
	console.log("Server Running at http://" + ipadd + ":" + port);
});

I hope you already cloned sample code from OpenShift server to your local PC. Now you should replace server.js file with your created one. Now do the Commit and finally Push to upload your code to the server and build the file (Push will do that in favor of you).
 

Now you should see your server running on 8080 port which is defined by OpenShift. You can check the server running message on public web url what you have defined in your application creation page. You should see something like that.
‘Welcome to NodeJS HTTP Server!
Server running at http://127.X.X.X:8080’
 

Working with NodeJS Module and Own Code:
NodeJS modules are most important things to do lots of our work easy. it’s much important to know development style for module. NodeJS Package Manager (npm) is most important and installer module for other modules. NPM always come as in box and primary installed module for NodeJS. You can check NPM version with command $ npm –version and you can install other modules using $npm install . You make like to know the uninstall process. Use $ npm uninstall to uninstall a module.
 

Now we should know where we can install modules. Most interactive thing is that we can install NodeJS module under NodeJS install directory or in our application directory itself. Please remember ‘node_modules’ directory is container of modules and it will automatically create first time when we install our first module.
 

As an example we like to create functionality like upload files with upload progress as a percentage. We know that BinaryJS is most useful module for that. You may visit http://binaryjs.com for more information. Now create or select a directory as your application directory. Open command prompt and move to the application directory. Now use command $ npm install BinaryJS and you will see module is installed under ‘node_module’ directory under your application directory. You should notice BinaryJS may depend on other modules and that dependency modules also be installed under BinaryJS directory within the same folder name as ‘node_module’.
 

So now how we can work with our BinaryJS. Now you can see we have an readymade example in our BinaryJS folder. Go to your application folder then node_module then BinaryJS and then example and finally fileupload. This is the example what already we have. And run command $ node server.js. You probably get some errors like ‘cannot find module exapress’ that does mean that, to run the file upload server we have dependency upon `express` module. Now install express module using $ npm install express command and you will see another directory named `node_module` and it contains newly installed module `express’. Now again run $node server.js and you will see something like `HTTP and BinaryJS server started on port 9000’ and finally our BinaryJS server is up and waiting for file to be upload. Now navigate with file explorer at yourapplication/node_module/BinaryJS/exmaples/fileupload/public/ and just click and open index.html on a browser. Now you can see a gray box. You can drag and drop a large file and you can see file uploading on progress. Finally you can get uploaded file at yourapplication/node_module/BinaryJS/exmaples/fileupload/public/. And you can move fileupload example in anywhere in your hard drive or project folder then change configuration and locations in server.js and index.html. With the above steps you can create your own file uploading system with real time upload progress using NodeJS.
 

You can run server.js (server) and index.html (client) in two different server as well. You can use different environment to upload something from one server to another one using NodeJS HTTP communication. To change the thing open index.html and change BinaryJS host url localhost to your NodeJS server’s host. Isn’t simple?


You can feel free to ask me any question or doubt. Or you may share something better to me as well. This is very easy to here, just create an account or sign in with your existing account and post your message.

Install and Working With NodeJS on Windows PC and OpenShift, 7.7 out of 10 based on 30 ratings



Sign Up     Sign In