Saturday, October 25, 2014

Why is JavaScript so beautiful?

There is a common belief among people that javascript does not have all the beauties of a modern programming language, like private, public and privileged function, in all information hiding.

I am providing a few examples which showcases the beauty and power of javascript::



I hope u liked the post. Leave comments if you have any suggestions or want to learn more.

Wednesday, June 11, 2014

Binding in JavaScript

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FFunction%2Fbind

Sunday, June 1, 2014

Great articles

http://www.devign.me/javascript-this-keyword/

http://snook.ca/archives/javascript/javascript_pass

Great article for how values are passed in JavaScript

http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language

Sunday, March 16, 2014

OOP in Node.js / JavaScript

So ECMA standards have support for two type of OO programming structures.

1) Classical: The regular class based structures.
2) Prototypical: Where u can use any function from anywhere, as they are not class based.

I have been reading these awesome content:

1) http://book.mixu.net/node/ch6.html
2) http://dmitrysoshnikov.com/ecmascript/chapter-7-1-oop-general-theory/
3) http://howtonode.org/prototypical-inheritance
4) http://javascriptweblog.wordpress.com/2011/05/31/a-fresh-look-at-javascript-mixins/


These people are awesome.

Thanks..!

Tuesday, March 11, 2014

How to make the EC2 instance of node.js or any application point to my registered domain name??

Considering an example from GoDaddy.com

1) Goto manage your DNS.
2) Goto A host settings.
3) Change the IP to your EC2 machines IP.
4) Changes can be instant or they can take upto 48 hours.
5) Enjoy!



Deploying Node.js Express application on AWS

AWS provides a free account for experimenting with their cloud.

Joyent is a competitor in terms of deploying Node.js applications.

Node.js + MongoDB = Awesome at Joyent (+ mongoLab).

Today I deployed my website on aws ec2.

Here are the steps:

1) Create an account on amazon.
2) Goto dashboard.
3) Create a new instance of amazon ec2. (its very intuitive and easy to use, lots of resources on google.)

4) Rerouting inbound port 80 to whichever port you are using in your application.
steps:
a) sudo bash
b) iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
c) Save to a file:
    iptables-save > /etc/iptables-up.rules
d) save it to startup configuration file:
echo "post-up /sbin/iptables-restore < /etc/iptables-up.rules" >> /etc/network/interfaces
e) Done.

5)Install node.js from there github repository.
take the stable release branch: https://github.com/joyent/node/tree/v0.10.26-release

6) install npm, express, mongoDB or any other modules you want to.

7) clone your application

8) Go back to aws. Goto security groups.
a) Add All ICMP port with source whatever you like or keep as default.
b) Open TCP port 3000.

9) Goto amazon ec2 instance. Spin your application.
10) Enter your public DNS with your port (here it is 3000).
11) Enjoy!

Tuesday, February 4, 2014

Best templating for Node JS using ExpressJS

I feel the best HTML template tool is EJS. As it is easier to understand as compared to the shorthand style used in Jade.

The three most important things when writing code using EJS are:

1. To print the value of a JavaScript expression as part of your HTML, while escaping any markup characters like <, >, and & to prevent XSS attacks, just use <%= ... %>, like this:
<h3><%= post.title %></h3>
As a good rule of thumb, this is the safest way to output variables in your markup.
2. To print the value of a JavaScript expression as part of your HTML without escaping markup characters (because you are intentionally printing out HTML markup), use <%- %>, like this:
<div class="post-body">

<!--- I hope you validated this HTML against XSS attacks! -->

<%- post.body %>

</div>
3. To simply run some JavaScript inside your template, use <% and %>, like this. Notice that we can shift back into "HTML mode" inside the callback function of our call to "_.each()". We'll switch back to "JavaScript mode" to close the _.each call:
<% _.each(posts, function(post) { %>

  <h3><%= post.title %></h3>

  <div class="post-body">

    <%- post.body %>

  </div>

<% }) %>
The _.each() function, as you've probably guessed, is an Underscore function that acts a lot like jQuery's each() function. It loops over all of our posts and invokes a function (usually written inline with the "function" keyword) once for each post.