Saturday, January 18, 2014

Basic Description About Node.js

Basic description about what node.js is.

Breaking the borders

No doubt, there has been a huge hype around Javascript for years now. It seems like Javascript is everywhere. You can create awesome mobile apps with it. Some crazy guys even created a project where C code will be translated into Javascript( emscripten ) See an awesome example of what’s possible with Javascript here.

If you can create singlepage-apps that works like a native application… Why shouldn’t you use Javascript on the server as well? That’s where node.js comes into play. I won’t talk about whether it’s a good idea to use an untyped language on the server or not. This decision belongs to you.. But… choose wisely;))

Node is written in C++ (as well as Google’s V8 Engine), runs on Googles V8 and is part of the Server Side JavaScript environnement. It extends the JavaScript API to offer usual server side functionalities. Node has been created by Ryan Dahl back in 2009. The project is run by Joyent (the company employing Ryan Dahl). It’s an opensource project..

Node’s internal architecture



The goal

“Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications.” That’s what the website says. It should be easy to use, and also very flexible.
There are 3 major feature in node’s architecture, to archive this goal.

Single threaded
Unlike the most (web)server which spawns a thread for every incomming request, node only uses one thread. This avoids context switching. You may think, 1 thread sounds like a bottle neck? Well consider reading about the C10k problem and go on reading ;)..

Non blocking I/O
Javascript is by design a non blocking language, so node uses it as one of it’s main features to avoid waiting for a response of long running processes. (e.g. database calls)
This allows node to handle a huge amount of traffic because the process will be handed over and the main thread is free to do new stuff..

But wait.. didn’t I just say it’s single threaded… Well in the one hand it is.. In the other it’s not.. Let’s have a closer look.

node_threading_model

Actually, the event loop is indeed single threaded. To be able to answere to new incomming requests as soon as possible, long running jobs will be handed over to internal worker threads.

Event Loop
The (single threaded) event loop uses Marc Lehmann’s libev. As mentioned above, long running jobs will be handed over to worker threads to be ready for the next job. You can think of it as it’s a queue of things to do. Things which takes a little longer will be done by others so that you have more time doing new stuff ;)

Summary

I’ve explained how node works internally and what kind of technic is used to achieve the goals mentioned above. Node actually is a Javascript runtime without a DOM, but with a lot of API’s, you would wish to have in a browser. One of the main features in my opinion is that you can do nearly everything, but you don’t have to. E.g you can create a little webserver that’s really just printing out “Hello World”. This webserver plus the actual “logic” would contain no more that 8 lines of code..

Friday, December 27, 2013

How to remove old lock file terminating mongodb

dileep@dileep-VirtualBox:~$ sudo mongod --dbpath /var/lib/mongodb/
Sat Dec 28 01:36:55.801
Sat Dec 28 01:36:55.803 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Sat Dec 28 01:36:55.804
Sat Dec 28 01:36:55.933 [initandlisten] MongoDB starting : pid=2656 port=27017 dbpath=/var/lib/mongodb/ 32-bit host=dileep-VirtualBox
Sat Dec 28 01:36:55.936 [initandlisten]
Sat Dec 28 01:36:55.937 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Sat Dec 28 01:36:55.937 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Sat Dec 28 01:36:55.938 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.
Sat Dec 28 01:36:55.939 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Sat Dec 28 01:36:55.939 [initandlisten]
Sat Dec 28 01:36:55.940 [initandlisten] db version v2.4.8
Sat Dec 28 01:36:55.941 [initandlisten] git version: a350fc38922fbda2cec8d5dd842237b904eafc14
Sat Dec 28 01:36:55.941 [initandlisten] build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Sat Dec 28 01:36:55.942 [initandlisten] allocator: system
Sat Dec 28 01:36:55.942 [initandlisten] options: { dbpath: "/var/lib/mongodb/" }
**************
Unclean shutdown detected.
Please visit http://dochub.mongodb.org/core/repair for recovery instructions.
*************
Sat Dec 28 01:36:55.951 [initandlisten] exception in initAndListen: 12596 old lock file, terminating
Sat Dec 28 01:36:55.952 dbexit:
Sat Dec 28 01:36:55.952 [initandlisten] shutdown: going to close listening sockets...
Sat Dec 28 01:36:55.953 [initandlisten] shutdown: going to flush diaglog...
Sat Dec 28 01:36:55.955 [initandlisten] shutdown: going to close sockets...
Sat Dec 28 01:36:55.956 [initandlisten] shutdown: waiting for fs preallocator...
Sat Dec 28 01:36:55.957 [initandlisten] shutdown: closing all files...
Sat Dec 28 01:36:55.959 [initandlisten] closeAllFiles() finished
Sat Dec 28 01:36:55.959 dbexit: really exiting now

To remove the Error we only do ,

dileep@dileep-VirtualBox:/$ cd var/lib/mongodb
dileep@dileep-VirtualBox:/var/lib/mongodb$ sudo rm mongod.lock
dileep@dileep-VirtualBox:/var/lib/mongodb$ ls
local.0  local.ns  _tmp

Then have to put the mongod --repair comand for repairing the mongodb. or else try to do the

 sudo mongod --dbpath /var/lib/mongodb/      command in the terminal.

Then the mongodb will work again.




Thursday, December 26, 2013

HTTPS server (SSL) Implementation in node.js

Node.js provides HTTP server as one of the core libraries. So there is no need for a separate HTTP server. But if you are developing any application in node js which needs secure transactions then HTTPS server is necessary for it. So it can allow private information to be transmitted without the problems of eavesdropping, data tampering, or message forgery between your node.js  server and your visitor’s browser.
Developer should add SSL certificate in code to implement HTTPS server in node.js. In my last post I have explained, how to create self-signed SSL certificate in Ubuntu. It has commands and detail explanation for generating self-signed SSL in any Unix OS.  So for testing purpose you do not need to buy SSL certificate.
You can implement HTTPS server using simple steps:
Prerequisites:
  • node.js
Before start to implement HTTPS server please make sure that you have installed node.js in your system.
Commands to Create self-signed SSL certificate 
If you need a detail explanation and process, you can get it here. Following are commands to generate self-signed SSL in any Unix OS :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Become root first

$ apt-get install openssl

$ mkdir /etc/ssl/self-signed && cd /etc/ssl/self-signed

$ openssl genrsa -des3 -out server.key 2048

$ openssl rsa -in server.key -out server.key.insecure

$ mv server.key server.key.secure && mv server.key.insecure server.key

$ openssl req -new -key server.key -out server.csr

$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Above commands will generate two files server.key(private key to SSL certificate) and server.crt(signed certificate) in /etc/ssl/self-signed folder.
Node.js Code to implement HTTPS server
Now create a file server.js in your system. And add following code in it :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Include the https and ,file system modules
var https = require('https');
var fs = require('fs');

//Create the server options object, specifying the SSL key & cert
var options = {
key: fs.readFileSync('/etc/ssl/self-signed/server.key'),
cert: fs.readFileSync('/etc/ssl/self-signed/server.crt')
};

//Create the HTTPS enabled server - listening on port 443
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(443);
Code Explanation :
Following is line by line explanation of above code:
1
2
var https = require('https');
var fs = require('fs');
In above two lines we are adding HTTPS and FS modules which are required for SSL implementation. After including require modules we need to add SSL files (key and cert files) path. We have already generated self-signed SSL and its files are stored in /etc/ssl/self-signed folder.
1
2
3
4
var options = {
key: fs.readFileSync('/etc/ssl/self-signed/server.key'),
cert: fs.readFileSync('/etc/ssl/self-signed/server.crt')
};
In above code we are adding server.key and server.crt files path in options array variable. Now we need to create HTTPS server and pass this files through options variable to it.
1
2
3
4
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(443);
In above code lines, we have created HTTPS server by using createServer method. In it we have passed options variable as a parameter which contains SSL files path. And this server is listening on port no 443 which is a HTTPS port.
If you buy authenticated SSL certificate from any trusted service provider then you will get .key and .crt files. Just save this files in your system and replace self-signed SSL files path with this new authenticated files in above code.
Now open terminal and goto path where you have saved server.js file in your system. And run following command:
1
node server.js
Now open your favourite browser and enter https://localhost in address bar and press enter key. You will get hello world text in browser. That means you have successfully implemented HTTPS server in node.js. Please note that if you are using any other port than 443 then please mention that port number in url. For example, if you are using port number 8443 then enter https://localhost: 8443 url in browser.


Monday, December 23, 2013

Create self-signed SSL certificate in UbuntuinShare
What is SSL Certificate?
SSL is an acronym for Secure Sockets Layer. SSL Certificates are small data files that digitally bind a cryptographic key to an organisation’s details. It creates an encrypted connection between your web server and your visitor’s browser, allowing for private information to be transmitted without the problems of eavesdropping, data tampering, or message forgery. When SSL gets install on web server,  it activates the padlock and the https protocol (over port 443).
What is Self-signed SSL Certificate?
Organisation needs to buy SSL certificate from trusted hosting companies and its prices are very high. So, for development and testing purpose organisation can use self-signed SSL certificates. This self-signed SSL are certificates which get locally create on web server where it needs for testing of new SSL implementation. It is an identity certificate signed by its own creator; however, they are considered to be less trustworthy.
This temporary certificate generates an error in the client browser to the effect that the signing certificate authority is unknown and not trusted because it’s not signed by any known trusted CA authority.
SSL Certificate Files:
SSL certificate needs .key and .crt files. This files represent both parts of a certificate, .key is a private and .crt is a public part of certificate that means key being the private key to the certificate and crt being the signed certificate.
How to create Self-Signed SSL certificate in Ubuntu?
The openssl library is required to generate self-signed SSL certificate in Ubuntu. Open terminal and become root first . Now check if you already have openssl installed. Use following command for it:
1
$ which openssl
If above command returns path like /usr/bin/openssl , that means your system has openssl. But if it does not return any path then you will need to install openssl yourself. Use following command to install openssl:
1
$ apt-get install openssl
After installing openssl, private key (.key file) and signed certificate (.crt file) is required for SSL certificate. We need to store .key and .crt files in a single folder.
1
$ mkdir /etc/ssl/self-signed && cd /etc/ssl/self-signed
Above commands create self-signed folder on /etc/ssl path. Following are simple four steps which will guide you to create self-signed SSL certificate for your web server.
Step 1 :  Create a Private Key
The first step is to create your RSA Private Key. This key will be 1024 bit RSA key which will be encrypted using Triple-DES and will be store in a PEM format so that it will be readable as ASCII text. Command to create RSA key:
1
$ openssl genrsa -des3 -out server.key 1024
Edited:
Due to the increasing computing power available to decrypt SSL certificates, the Certificate Authority Browser (CAB) Forum (the entity that establishes SSL industry standards) requires that all SSL certificates issued after Jan. 1, 2014, use at least 2048-bit keys. SSL certificates that use 1024-bit keys are no longer secure. Command to create 2048 bit RSA key:
1
$ openssl genrsa -des3 -out server.key 2048
Step 2 : Remove pass-phrase from key
One side-effect of private key is that, Apache always ask for pass-phrase when web server gets start. But we can remove this pass-phrase restriction using following command:
1
$ openssl rsa -in server.key -out server.key.insecure
Now rename files:
1
$ mv server.key server.key.secure && mv server.key.insecure server.key
Step 3 : Generate a CSR (Certificate Signing Request)
Once private key is generated and pass-phrase restriction is removed, you should generate certificate signing request(CSR). During CSR generation, you will get prompted for different information which are the certificates attribute. Please note when you will get prompt for “Common Name”, you should enter complete name of your domain. For example if request is getting generate for shivalibari.com then please enter shivalibari.com as a common name. So this domain name will get protected with SSL like : https://shivalibari.com. Use following command for CSR generation:
1
$ openssl req -new -key server.key -out server.csr
Step 4 : Generate Self-Signed SSL Certificate
Use following command to generate self-signed certificate:
1
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
This certificate can get use for 365 days.
Using above simple four steps you can successfully create self-signed SSL certificate. You can use the filesserver.key and server.crt which are located in /etc/ssl/self-signed into your code or in any programming language to implement SSL or HTTPS connection with your web application.


Monday, November 18, 2013

Principle : 82


If you follow these principles, you will succeed on many levels, engendering an atmosphere of trust and loyalty.  Working in such an atmosphere, the group will feel secure at a basic level that is very necessary. Insecurity creates massive stress and all the problems that attend it.

But we have to be realistic, too. Today more than ever, it takes consciousness to keep on the responsible track. For many in business, responsibility has become an old-fashioned value to be shrugged off in favour of profitability.  The financial crash of 2008 was engineered through a flagrant lack of responsibility, combined with risk-taking far out of bounds with sensible practice. Yet the lesson that the financial sector took away was the opposite of responsible. With record profits and huge bonuses in the offing, they went back to a slightly modified version of their worst practices.

Wednesday, July 10, 2013

Principle 81 :

Feel Your Fear and Do It Anyway
Every successful person I know has been willing to take a leap of faith even though they were afraid. They knew that if they didn’t act, opportunity would pass them by.
Recognize fear for what it is: a mental trick that your ego uses in attempt to protect you from the negative outcomes it imagines. You create your fear and you have the power to dissolve it as well. Use the techniques outlined in this article to overcome this powerful roadblock … so you can turn your dreams into reality and live the life you deserve. Remember, no one achieves greatness by playing it safe.

Wednesday, July 3, 2013

Principle 80 :

3 Ways to Overcome Fear
Here are three easy techniques for moving past your fears:
1.    Disappear fear by choosing a positive mental image. When we’re afraid, our minds are full of negative thoughts and images. When you are feeling afraid, tune into the images in your head. Then choose to replace them with a positive image of your desired outcome. For example, if you’re afraid that starting your own business will end in bankruptcy and losing your house, instead picture your new business becoming wildly successful and buying a second vacation home with all of the added income you’ll be earning in your new company.

2.    Focus on the physical sensations. You may feel fear in your body as a sinking feeling in your stomach, a tightening in your shoulders and chest, or an elevated heart rate. Next, focus on the feelings you’d rather be experiencing instead, such as peace and joy. Fix these two different impressions in your mind’s eye, then move back and forth between the two, spending 15 seconds or so in each. After a minute or two, you’ll find yourself feeling neutral and centered.
3.    Recall your successes. You’ve overcome countless fears to become the person you are today, whether it was learning to ride a bike, driving a car for the first time, or kissing someone for the first time. New experiences always feel a little scary. But when you face your fears and do them anyway, you build up confidence in your abilities. The situation you’re facing now and how your fear is manifesting may be different than what you’ve experienced in the past, but you know how to overcome your fears. You’ve spent a lifetime doing so successfully.