Jekyll2021-08-09T03:17:33+00:00http://www.codehenge.net/feed.xmlCodehengeWho built it, and why? A blog about software, technology, and moreConstantine Aaron CoisReboot2021-08-08T22:39:00+00:002021-08-08T22:39:00+00:00http://www.codehenge.net/2021/08/reboot<p>Its been a while. A long while.</p>
<p>There’s a momentum to writing. You write, you exercise those muscles, and it helps you to write more. Makes it easier to write the next time you sit down. But, just like a muscle, if you let your writing practice attrify, it becomes very hard to sit down and put anything down. Hard to even formulate ideas. So it goes.</p>
<p>This is a trap I fell into, and its time to dig my way out. Just like exercise, good diet, or starting any new habit - the only way to start is to start. Its just that simple and easy and incredibly difficult. So here it is, my public declaration of my attempt to get back to regular posting. I have a lot to say, and like to think I can provide value. Expect to hear more from me soon, and if anyone has questions or things they’d like to hear about, feel free to reach out.</p>{"login"=>"cacois", "email"=>"cacois@gmail.com", "display_name"=>"cacois", "first_name"=>"Constantine", "last_name"=>"Cois"}cacois@gmail.comIts been a while. A long while...So here it is, my public declaration of my attempt to get back to regular posting....What is Node.js?2014-01-06T03:58:31+00:002014-01-06T03:58:31+00:00http://www.codehenge.net/2014/01/what-is-node-js<p>Node.js - It's new, its exciting...but what, exactly, is it?</p>
<p>You may have heard of Node.js, but know only that it has something to do with “real-time” or “highly scalable” apps. You may have heard that Node.js is JavaScript for the server-side (and you may be wondering why anyone would want that!). Or maybe you know exactly what Node.js is, but aren’t sure when or why to use it. Just sit back, and I’ll explain everything.</p>
<p>Let's start at the beginning...</p>
<h2>The Web is Changing</h2>
<p>The web used to be about consumption. Viewing web pages, watching videos, looking at pictures of cats. Of course, its still about pictures of cats…but the web has increasingly become about interaction. Users around the world want to interact with each other, and they want to do it in real time. Chat, gaming, constant social media updates, collaboration – each of these features requires real-time communication between users, clients, and servers across the web. What’s more, this real-time communication needs to happen at massive scale, supporting hundreds, thousands, even millions of users.</p>
<p>So what do software developers need to make this happen? We need real-time communication between clients and servers – which means we need fast, persistent I/O. Anyone with web development experience knows that HTTP wasn’t built with this use case in mind. Large numbers of clients continuously polling a server simultaneously is incredibly slow and inefficient. To enable scalable real-time communication, servers need to be able to push data to clients, instead of HTTP’s heavy request/response model. We also need to make sure that these lightweight push communications work in a way that is scalable, maintainable, and usable from a software development standpoint.</p>
<h2>Enter Node.js</h2>
<p>Node.js is an event-driven, server-side JavaScript environment. Node runs JavaScript using the V8 engine developed by Google for use in their Chrome web browser. Leveraging V8 allows Node to provide a server-side runtime environment that compiles and executes JavaScript Really FastTM. The major speed increase is due to the fact that V8 compiles JavaScript into native machine code, instead of interpreting it or executing it as bytecode. Node.js and the V8 engine are both open source and cross-platform, running on Mac OSX, Windows, and Linux.</p>
<p>But JavaScript? On the server-side? Why? Though JavaScript has traditionally been relegated to menial tasks in the web browser, it’s actually a fully-functional programming language, capable of anything that more traditional languages like C++. Ruby, or Java, are. Furthermore, JavaScript has the advantage of an excellent event model, ideal for asynchronous programming. JavaScript is also a ubiquitous language, well known by millions of developers. This lowers the learning curve of Node.js, since most devs won’t have to learn a new language to start building Node.js apps.</p>
<p><a href="https://www.udemy.com/learn-nodejs-by-example/?couponCode=codehenge">For a limited time, subscribe to Learn Node.js by Example now and get 50% off!</a></p>
<h2>Asynchronous Programming The Easy Way</h2>
<p>In addition to lightning fast JavaScript execution, the real magic behind Node.js is something called the Event Loop. To scale to large volumes of clients, all I/O intensive operations in Node.js are performed asynchronously. The traditional threaded approach to asynchronous code is cumbersome and creates a non-trivial memory footprint for large numbers of clients (each client spawns a thread, each thread uses dedicated memory, the memory usage adds up). To avoid this inefficiency, as well as the known difficulty of programming threaded applications, Node.js maintains an event loop which manages all asynchronous operations for you. When a Node application needs to perform a blocking operation (I/O operations, heavy computation, etc) it sends an asynchronous task to the event loop, along with a callback function, and then continues to execute the rest of its program. The event loop keeps track of the asynchronous operation, and executes the given callback when it completes, returning it’s results to the application. This allows you to manage a large number of operations, such as client connections or computations, letting the event loop efficiently managing the thread pool and optimize task execution. Of course, leaving this responsibility to the event loop makes life particularly easy for Node.js developers, who can then focus on their application functionality.</p>
<p><figure>
<img src='/assets/node-event-loop-e1388980560411.png' alt='The Node.js Event Loop Lifecycle' width='600' height='449'/>
<figcaption>The Node.js Event Loop Lifecycle</figcaption>
</figure>
<p>
<p>This capability to simplify asynchronous programming is what makes Node.js such a powerful tool for developers. With Node.js, you can build complex applications that can scale to millions of client connections because the application handling client requests is passing off all of the time-intensive work of managing I/O and computation to the event loop.</p>
<h2>The Node.js Community</h2>
<p>In addition to it’s innate capabilities, Node.js has a thriving open source community which has produced many excellent modules to add additional capabilities to Node.js applications. One of the most famous is Socket.io, a module to manage persistent connections between client and server, enabling the server to push real-time updates to clients. Socket.io abstracts the technology used to maintain these connections away from the developer, automatically using the best technology available for a particular client (websockets if the browser supports it, JSONP or Ajax longpolling if not). This amazing technology allows you to program real-time applications simply, with very little code. For example, take this highly-scalable, real-time chat server:</p>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="nx">server</span><span class="p">.</span><span class="nx">listen</span><span class="p">(</span><span class="mi">8080</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">io</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">socket.io</span><span class="dl">'</span><span class="p">).</span><span class="nx">listen</span><span class="p">(</span><span class="nx">server</span><span class="p">);</span>
<span class="nx">io</span><span class="p">.</span><span class="nx">sockets</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="dl">'</span><span class="s1">connection</span><span class="dl">'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">socket</span><span class="p">){</span>
<span class="c1">// when message received, send it to all connected clients</span>
<span class="nx">socket</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="dl">'</span><span class="s1">message</span><span class="dl">'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">message</span><span class="p">){</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">"</span><span class="s2">Received message: </span><span class="dl">"</span> <span class="o">+</span> <span class="nx">message</span> <span class="o">+</span>
<span class="dl">"</span><span class="s2"> - from client </span><span class="dl">"</span> <span class="o">+</span> <span class="nx">socket</span><span class="p">.</span><span class="nx">id</span><span class="p">);</span>
<span class="nx">io</span><span class="p">.</span><span class="nx">sockets</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="dl">'</span><span class="s1">chat</span><span class="dl">'</span><span class="p">,</span> <span class="nx">socket</span><span class="p">.</span><span class="nx">id</span><span class="p">,</span> <span class="nx">message</span><span class="p">);</span>
<span class="p">});</span>
<span class="p">});</span></code></pre></figure>
<p>Just 10 lines of code! The accompanying client-side code is just as simple. Imagine the possibilities!</p>{"login"=>"cacois", "email"=>"cacois@gmail.com", "display_name"=>"cacois", "first_name"=>"Constantine", "last_name"=>"Cois"}cacois@gmail.comNode.js - It's new, its exciting...but what, exactly, is it? Let's start at the beginning...Discount for Online JavaScript Conference 20132013-09-27T13:44:25+00:002013-09-27T13:44:25+00:00http://www.codehenge.net/2013/09/discount-for-online-javascript-conference-2013<p>I’ll be speaking at the Environments for Humans JavaScript Summit on November 21st. The conference is a 3-day, all online conference, which means no travel hassle/cost, and you get to watch great talks from the comfort of home!</p>
<p>It's shaping up to be a great lineup (check out the <a href="http://environmentsforhumans.com/2013/javascript-summit/">list of talks!</a>), and your registration includes copies of slides and access to recordings of talks after the event.</p>
<p>The organizers have given me a discount code to share with you: Use 20COIS to get 20% off individual or meeting room tickets. Enjoy!</p>{"login"=>"cacois", "email"=>"cacois@gmail.com", "display_name"=>"cacois", "first_name"=>"Constantine", "last_name"=>"Cois"}cacois@gmail.comI’ll be speaking at the Environments for Humans JavaScript Summit on November 21st. The conference is a 3-day, all online conference, which means no travel hassle/cost, and you get to watch great talks from the comfort of home! It's shaping up to be a great lineup (check out the list of talks!), and your registration includes copies of slides and access to recordings of talks after the event. The organizers have given me a discount code to share with you: Use 20COIS to get 20% off individual or meeting room tickets. Enjoy!Automate Your Development Environment With Vagrant2013-02-10T17:12:19+00:002013-02-10T17:12:19+00:00http://www.codehenge.net/2013/02/automate-your-development-environment-with-vagrant<p><figure><img src='assets/works-on-my-machine-starburst1-e1359931607861.png' alt='100% Certified!' width='200' height='193'/><figcaption>100% Certified!</figcaption></figure><h2>Works on My Machine!</h2>
<p>I'd like to talk about my favorite software development meme: The 100%, certified, "Works on my machine!" build. We've all seen it, and it's funny every time. Take a second and consider how many times, in fact, you have seen this situation. You write and test your code thoroughly, and it all goes to hell when you commit to your build environment. Or worse, everything's working fine in your staging environment, and something breaks unexpectedly when you push to production. How much frustration can be chalked up such scenarios? How much time have you lost?</p>
<p>Let's explore how this phenomenon occurs. The root cause is a lack of parity between environments throughout your software development process. Your development environment is not the same as your test environment. Your staging environment differs from your production environment. This happens naturally, especially with development machines. Developers work on many projects, simultaneously or over the course of time. They pile up applications, libraries, and different versions of tools, much in the same way a basement workshop becomes cluttered after years of home repair projects. Consider your average developer. Maybe they get a new machine and start working on a Java project. They install Java6, Tomcat6, Spring, etc. Then a few more project come along, and Java7, Tomcat7, Python 2.7.2, Django, and C++ are installed. Of course, our developer wants to keep up on the latest in the field, so they also install Node.js, Clojure, MongoDB, Python3, and C++11 to tinker with. And that's not to mention other, ahem, <a href="https://minecraft.net/">exercises</a> <a href="http://store.steampowered.com/">in</a> <a href="http://fallout.bethsoft.com/">mental</a> <a href="http://www.worldofwarcraft.com">acuity</a>. Unfortunately, this leads to a fairly cluttered system, with plenty of potential for unanticipated software interactions
<div>
<p><figure><img src='assets/cluttered-system-e1360372808569.png' alt='Cluttered system' width='520' height='390'/><figcaption></a> What could go wrong?</figcaption></figure></div>
<p>On top of this, developers prefer different development environments. Some prefer OSX, some windows, some a particular flavor of linux. Just try to get an Ubuntu fan to code in Windows. Go ahead, I'll wait. And, of course, different IDEs, tools, debuggers, network configurations... Each installed application, each change in configuration, causes divergence from the clean operating environment you desire for development and testing. The environment that matches your pristine staging and production servers. So what to do? The ideal solution would create unique, reproducible environments for each project. Each developer's environment would be identical, as would the staging and production servers.</p>
<h2>Enter Vagrant...</h2>
<p><figure><img src='assets/vagrant-e1360203410345.png' alt='vagrant' width='200' height='200'/><figcaption></a></p></figcaption></figure><p>Vagrant leverages virtualization and automated system configuration to make this dream a reality. With VirtualBox and its comprehensive API under the hood, Vagrant creates and manages custom virtual environments to your exact specification. But wait, we've been able to create virtual machines for years. What's new here? The problem is configuration of a brand new virtual macine for each project is a massive chore, reinstalling all of your dev tools each time sounds like torture, and developers will still each want VMs of different operating systems...what are we solving? Vagrant does it differently. By giving you the option to leverage powerful, proven automated configuration technologies such as <a href="http://www.opscode.com/chef/">Chef</a> or <a href="https://puppetlabs.com/">Puppet</a> (as well as your own custom shell scripts, if you like), Vagrant takes the time and tedium out of configuring a virtual environment. Need a Ruby on Rails development environment? Use <a href="http://community.opscode.com/cookbooks/rails_rvm">this open source Chef cookbook</a> already designed by someone else. Python and Django? <a href="http://community.opscode.com/cookbooks/django">Yo</a>. How about Node.js? Here, <a href="https://github.com/cacois/vagrant-node-mongo">I'll give you mine</a>. Don't you love FOSS? But your development tools, such as IDEs, browsers, etc, and the preferred operating systems...this is the best part. By clever use of shared directories and port mapping, Vagrant allows you to keep your code on your host machine, with all of your current tools and apps. You can even run and debug your web application in your own host browser. The VM running in the background, serving up your app, is transparent to you, doing nothing but running your app in a custom, controlled environment.</p>
<p>Let's try a quick demo, to make this workflow completely clear. First, download and install <a href="https://www.virtualbox.org/">VirtualBox</a> (if you don't have it already) and <a href="http://www.vagrantup.com/">Vagrant</a>. Vagrant will be creating a custom virtual machine for you, based on a configuration i'll give you. To do this, Vagrant will need a template, or base VM, to copy and customize. The vagrant dev team is nice enough to provide a number of base linux VMs for our use. To install one, run the following command:</p>
<p><code>$ vagrant box add precise32 http://files.vagrantup.com/precise32.box</code></p>
<p>This will download a base VM of Ubuntu 12.04 32-bit, about 250MB in size, and refer to it by the name "precise32". Next, you'll need a project to work on, and a development environment configuration. Here's a Node.js demo I have prepared for you. Clone this repository, and move into the project directory:</p>
<p><code>$ git clone https://github.com/cacois/vagrant-node-mongo.git<br />
$ cd vagrant-node-mongo</code></p>
<p>By the way, are you interested in <strong>mastering Node.js</strong>? Check out my online course <a href="http://www.udemy.com/learn-nodejs-by-example/">Learn Node.js by Example</a> for detailed screencasts and example applications. Sign up today from <a href="http://www.udemy.com/learn-nodejs-by-example/?couponCode=codehenge">this link and receive 50% off</a>!</p>
<p>Take a look inside this directory.</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">vagrant-node-mongo /
README.md
Vagrantfile
/app
/cookbooks</code></pre></figure>
<p>Inside the 'app' subdirectory, I have have a small node.js app that returns some text output to an HTTP request. In 'cookbooks' are some Chef cookbooks, or collections of recipes that define automated configuration commands to install Node.js, MongoDB, apt-get, and other essential tools. Let's take a closer look at the file named 'Vagrantfile'. This is the Vagrant configuration file, defining how to set up your custom project VM. It should look like this:</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="no">Vagrant</span><span class="o">::</span><span class="no">Config</span><span class="p">.</span><span class="nf">run</span> <span class="k">do</span> <span class="o">|</span><span class="n">config</span><span class="o">|</span>
<span class="n">config</span><span class="p">.</span><span class="nf">vm</span><span class="p">.</span><span class="nf">box</span> <span class="o">=</span> <span class="s2">"precise32"</span>
<span class="n">config</span><span class="p">.</span><span class="nf">vm</span><span class="p">.</span><span class="nf">forward_port</span> <span class="mi">3000</span><span class="p">,</span> <span class="mi">3000</span>
<span class="n">config</span><span class="p">.</span><span class="nf">vm</span><span class="p">.</span><span class="nf">share_folder</span> <span class="s2">"app"</span><span class="p">,</span> <span class="s2">"/home/vagrant/app"</span><span class="p">,</span> <span class="s2">"app"</span>
<span class="c1"># allow for symlinks in the app folder</span>
<span class="n">config</span><span class="p">.</span><span class="nf">vm</span><span class="p">.</span><span class="nf">customize</span> <span class="p">[</span><span class="s2">"setextradata"</span><span class="p">,</span> <span class="ss">:id</span><span class="p">,</span> <span class="s2">"VBoxInternal2/SharedFoldersEnableSymlinksCreate/app"</span><span class="p">,</span> <span class="s2">"1"</span><span class="p">]</span>
<span class="n">config</span><span class="p">.</span><span class="nf">vm</span><span class="p">.</span><span class="nf">customize</span> <span class="p">[</span><span class="s2">"modifyvm"</span><span class="p">,</span> <span class="ss">:id</span><span class="p">,</span> <span class="s2">"--memory"</span><span class="p">,</span> <span class="mi">512</span><span class="p">]</span>
<span class="n">config</span><span class="p">.</span><span class="nf">vm</span><span class="p">.</span><span class="nf">provision</span> <span class="ss">:chef_solo</span> <span class="k">do</span> <span class="o">|</span><span class="n">chef</span><span class="o">|</span>
<span class="n">chef</span><span class="p">.</span><span class="nf">cookbooks_path</span> <span class="o">=</span> <span class="s2">"cookbooks"</span>
<span class="n">chef</span><span class="p">.</span><span class="nf">add_recipe</span> <span class="s2">"apt"</span>
<span class="n">chef</span><span class="p">.</span><span class="nf">add_recipe</span> <span class="s2">"mongodb"</span>
<span class="n">chef</span><span class="p">.</span><span class="nf">add_recipe</span> <span class="s2">"build-essential"</span>
<span class="n">chef</span><span class="p">.</span><span class="nf">add_recipe</span> <span class="s2">"nodejs::install_from_package"</span>
<span class="n">chef</span><span class="p">.</span><span class="nf">json</span> <span class="o">=</span> <span class="p">{</span>
<span class="s2">"nodejs"</span> <span class="o">=></span> <span class="p">{</span>
<span class="s2">"version"</span> <span class="o">=></span> <span class="s2">"0.8.0"</span>
<span class="c1"># uncomment the following line to force</span>
<span class="c1"># recent versions (> 0.8.4) to be built from</span>
<span class="c1"># the source code</span>
<span class="c1"># , "from_source" => true</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">end</span>
<span class="k">end</span></code></pre></figure>
<p>Let's go through the config file to understand what its doing. First, we are telling Vagrant to use the base box named 'precise32' you downloaded a few moments ago. Then we configure port forwarding between the Vagrant VM and our host over port 3000, and a shared folder in which we will put our application code. Then some VM hardware settings, such as RAM configuration. Finally, we configure the automation system (chef_solo, in this case), and add the recipes from our '/cookbooks' directory defining the applications and services we want to install in the Vagrant VM. Note that I can also pass in specific configurations in JSON format - I'm using this to specify the exact version of Node.js (0.8.0) I want to install.</p>
<p>To power on your custom VM, from inside your application directory issue the following command:</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="nv">$ </span>vagrant up</code></pre></figure>
<p>If this is the first time you have issued this command for a new project, Vagrant will actually create the VM from scratch, which may take a few moments. The next time you enter the command, it will just power the VM on, which is substantially faster. When the 'vagrant up' command is issued, Vagrant works in the background to stand up the VM required for that environment (as defined in the Vagrantfile). If you open up the VirtualBox UI you can actually watch the VM appear, but there's no real reason to do so - you won't need the UI to interact with your VM. Instead, once the VM is up, type:</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="nv">$ </span>vagrant ssh</code></pre></figure>
<p>This will automatically give you an ssh session into your Vagrant VM, without the need to enter a hostname or credentials. You should see console output similar to the following:</p>
<p><figure><img src='assets/vagrant-up.png' alt='vagrant-ssh' width='577' height='157'/><figcaption></a></p></figcaption></figure><p>As you can see, Vagrant has created and ssh session for me into my Vagrant VM. This is the running environment for your application. It's a clean VM, with only a base operating system and the exact applications you configured installed. Issue the following commands to verify the installation of Node.js, NPM, and MongoDB:</p>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="nx">$</span> <span class="nx">node</span><span class="o">-</span><span class="nx">v</span>
<span class="nx">$</span> <span class="nx">npm</span> <span class="o">-</span><span class="nx">v</span>
<span class="nx">$</span> <span class="nx">mongo</span> <span class="o">-</span><span class="nx">version</span></code></pre></figure>
<p>if you do an 'ls', you will also notice a directory '/app', located in your vagrant home directory. Inside is your application code - these files are actually located on your host system, but are visible on your vagrant VM as a VirtualBox shared directory. This is where you will run your code. The shared directory means I can pull up Sublime Text 2 in my host OS (OSX), and edit the files that will be run in the Vagrant VM. I could start the test application now to see it running, but to get an optimal workflow, I want something that will restart my application when changes are made to the code. To get this functionality in Node.js, go ahead and install supervisor on your Vagrant VM from the ssh command line:</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="nv">$ </span><span class="nb">sudo </span>npm <span class="nb">install </span>supervisor <span class="nt">-g</span></code></pre></figure>
<p>(Note: I could eventually roll this installation into my chef recipes that set up this environment, to save myself a step)</p>
<p>Now launch the application:</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="nv">$ </span>supervisor app/app.js</code></pre></figure>
<p>Open up a text editor or IDE in your host system, and open the vagrant-node-mongo/app/app.js file for editing. Now here's a really cool part. Open a browser in your host system, and navigate to http://localhost:3000. Since your Node.js app is running in your Vagrant VM, you will see the following:</p>
<p><figure><img src='assets/vagrant-hello-world-e1360514201913.png' alt='vagrant-hello-world' width='500' height='253'/><figcaption></a></p></figcaption></figure><p>The application is running in your Vagrant VM, but you can test it on your host browser, just as if it were running locally! Now go into your text editor and change the app.js code from</p>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kd">var</span> <span class="nx">http</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">http</span><span class="dl">'</span><span class="p">);</span>
<span class="nx">server</span> <span class="o">=</span> <span class="nx">http</span><span class="p">.</span><span class="nx">createServer</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">res</span><span class="p">.</span><span class="nx">writeHead</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="p">{</span><span class="dl">"</span><span class="s2">Content-Type</span><span class="dl">"</span><span class="p">:</span> <span class="dl">"</span><span class="s2">text/html</span><span class="dl">"</span><span class="p">});</span>
<span class="nx">res</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="dl">"</span><span class="s2"><html><body><h1>Hello, World!</h1></body></html></span><span class="dl">"</span><span class="p">);</span>
<span class="nx">res</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
<span class="p">}).</span><span class="nx">listen</span><span class="p">(</span><span class="mi">3000</span><span class="p">);</span></code></pre></figure>
<p>to</p>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kd">var</span> <span class="nx">http</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">http</span><span class="dl">'</span><span class="p">);</span>
<span class="nx">server</span> <span class="o">=</span> <span class="nx">http</span><span class="p">.</span><span class="nx">createServer</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">res</span><span class="p">.</span><span class="nx">writeHead</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="p">{</span><span class="dl">"</span><span class="s2">Content-Type</span><span class="dl">"</span><span class="p">:</span> <span class="dl">"</span><span class="s2">text/html</span><span class="dl">"</span><span class="p">});</span>
<span class="nx">res</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="dl">"</span><span class="s2"><html><body><h1>Hello, from Vagrant!</h1></body></html></span><span class="dl">"</span><span class="p">);</span>
<span class="nx">res</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
<span class="p">}).</span><span class="nx">listen</span><span class="p">(</span><span class="mi">3000</span><span class="p">);</span></code></pre></figure>
<p>Then refresh your browser window. You will see your changes reflected in the running application.</p>
<p><figure><img src='assets/hello-from-vagrant-e1360514820793.png' alt='hello-from-vagrant' width='500' height='315'/><figcaption></a></p></figcaption></figure><p>This is the real power of Vagrant - I can code on my local machine, using my tools, applications, IDE, etc. I can run the application on my custom, controller Vagrant VM with a few short commands, and test the app using my favorite local browsers and testing tools. I"m working in a VM, but really don't even have to know about it. Vagrant handles all the management of the VM for me. When I'm done working, I just exit out of the Vagant ssh session:</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="nv">$ </span><span class="nb">exit</span></code></pre></figure>
<p>and tell Vagrant to power down the VM:</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="nv">$ </span>vagrant halt</code></pre></figure>
<p>Then I can move on to another project. I can have a Vagrant VM for each project I'm working on. Moreover, this is a <em>revolution</em> for team workflows. I can check my Vagrantfile and cookbooks into source control, just as I did with the github application we've been working on, and I can be assured that each member of my team will be developing his code against exactly the same runtime environment. What's more, since we are using Chef to do the VM configuration, I can even use the same Chef cookbooks to configure my centralized test servers, my staging environment, or even production. If we change something in the runtime environment for the application, each developer issues a vagrant destroy command:</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="nv">$ </span>vagrant destroy</code></pre></figure>
<p>and then creates a new VM with the updated configurations. This process rarely takes more than 2 minutes, and results in a completely fresh, 100% matching environment for the entire team, across the entire project workflow.</p>
<p>I can be absolutely certain that I'll never again hear "it works on my machine", because I'll know that every machine running my code is exactly the same.</p>
<p><strong>If you liked this article, help me out by sharing a 50% discount to my Node.js course here: <a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.udemy.com/learn-nodejs-by-example/?couponCode=codehenge" data-text="Sign up for Learn Node.js by Example now and get 50% off!" data-via="AaronCois" data-size="large" data-hashtags="nodejs">Tweet</a> Thanks!</strong></p>
<p>You should follow me on Twitter here: <a href="https://twitter.com/AaronCois" class="twitter-follow-button" data-show-count="false" data-show-screen-name="false">Follow @AaronCois</a></p>{"login"=>"cacois", "email"=>"cacois@gmail.com", "display_name"=>"cacois", "first_name"=>"Constantine", "last_name"=>"Cois"}cacois@gmail.comVagrant leverages virtualization and automated system configuration to deliver you customized virtual environments for your development projects. With VirtualBox and its comprehensive API under the hood, Vagrant creates and manages custom virtual environments to your exact specification. But wait, we've been able to create virtual machines for years. What's new here? The problem is configuration of a brand new virtual macine for each project is a massive chore, reinstalling all of your dev tools each time sounds like torture, and developers will still each want VMs of different operating systems...what are we solving? Vagrant does it differently. By giving you the option to leverage powerful, proven automated configuration technologies such as Chef or Puppet (as well as your own custom shell scripts, if you like), Vagrant takes the time and tedium out of configuring a virtual environmentNode.js Tutorial: Hello, World!2012-10-13T02:48:58+00:002012-10-13T02:48:58+00:00http://www.codehenge.net/2012/10/node-js-tutorial-hello-world<p>This is the introductory screencast from my online course, <a href="http://www.udemy.com/learn-nodejs-by-example/" title="Learn Node.js by Example">Learn Node.js by Example</a>! In this screencast, I present the basics of Node.js, and develop a small "Hello, World!" app. Enjoy!</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/H3aZ8h1gEf4" frameborder="0" allowfullscreen></iframe>
<p><strong>Get 50% off my Node.js course <a href="http://www.udemy.com/learn-nodejs-by-example/?couponCode=codehenge">here</a></strong></p>
<p>If you liked this article, help me out by sharing a 50% discount to my Node.js course here: <a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.udemy.com/learn-nodejs-by-example/?couponCode=codehenge" data-text="Sign up for Learn Node.js by Example now and get 50% off!" data-via="AaronCois" data-size="large" data-hashtags="nodejs">Tweet</a> Thanks!</p>
<p>You should follow me on Twitter here: <a href="https://twitter.com/aaroncois" class="twitter-follow-button" data-show-count="false">Follow @aaroncois</a><br />
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script></p>{"login"=>"cacois", "email"=>"cacois@gmail.com", "display_name"=>"cacois", "first_name"=>"Constantine", "last_name"=>"Cois"}cacois@gmail.comThis is the introductory screencast from my online course, Learn Node.js by Example! In this screencast, I present the basics of Node.js, and develop a small "Hello, World!" app. Enjoy!Screencast: Node.js and MongoDB Using MongoJS2012-10-09T01:55:22+00:002012-10-09T01:55:22+00:00http://www.codehenge.net/2012/10/screencast-node-js-and-mongodb-using-mongojs<p>Here is a free screencast from my online course, <a href="http://www.udemy.com/learn-nodejs-by-example/" title="Learn Node.js by Example">Learn Node.js by Example</a>! In this screencast, I present the basics of using the MongoJS module to add MongoDB to your Node.js applications. Enjoy!</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/j0mUoe8eghc" frameborder="0" allowfullscreen></iframe>
<p><strong>Get 50% off my Node.js course <a href="http://www.udemy.com/learn-nodejs-by-example/?couponCode=codehenge">here</a></strong></p>
<p>If you liked this article, help me out by sharing a 50% discount to my Node.js course here: <a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.udemy.com/learn-nodejs-by-example/?couponCode=codehenge" data-text="Sign up for Learn Node.js by Example now and get 50% off!" data-via="AaronCois" data-size="large" data-hashtags="nodejs">Tweet</a> Thanks!</p>
<p>You should follow me on Twitter here: <a href="https://twitter.com/AaronCois" class="twitter-follow-button" data-show-count="false" data-show-screen-name="false">Follow @AaronCois</a></p>{"login"=>"cacois", "email"=>"cacois@gmail.com", "display_name"=>"cacois", "first_name"=>"Constantine", "last_name"=>"Cois"}cacois@gmail.comHere is a free screencast from my online course, Learn Node.js by Example! In this screencast, I present the basics of using the MongoJS module to add MongoDB to your Node.js applications. Enjoy!Using Socket.io With Express 3.x2012-08-25T19:39:58+00:002012-08-25T19:39:58+00:00http://www.codehenge.net/2012/08/using-socket-io-with-express-3-x<p><figure><img src='assets/socketio9-e1345921380351.png' alt='' width='300' height='102'/><figcaption></a>Excited about version 3.x of Express, the Node.js web framework? It has some great <a href="https://github.com/visionmedia/express/wiki/New-features-in-3.x" title="Express 3.x Features">new features</a>, and is certainly worth migrating to. Many of my apps make use of <a href="http://socket.io">Socket.io</a>, one of my favorite Node modules, and in this case, and as of now (Socket.io v9.1) the migration isn't as smooth as it could be. Until socket.io catches up with the changes to Express, here's how you can use it with Express 3.x.<br</figcaption></figure>Back in Express 2.x, you would create an express object:</p>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kd">var</span> <span class="nx">express</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">express</span><span class="dl">'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">app</span> <span class="o">=</span> <span class="nx">express</span><span class="p">.</span><span class="nx">createServer</span><span class="p">();</span></code></pre></figure>
<p>and then configure your socket.io object to listen along with the express app:</p>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kd">var</span> <span class="nx">io</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">socket.io</span><span class="dl">'</span><span class="p">).</span><span class="nx">listen</span><span class="p">(</span><span class="nx">app</span><span class="p">);</span></code></pre></figure>
<p>then tell the express app to listen on a port:</p>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="nx">app</span><span class="p">.</span><span class="nx">listen</span><span class="p">(</span><span class="mi">8080</span><span class="p">);</span></code></pre></figure>
<p>One of the first changes in Express 3.x is that express.createServer() has been deprecated. If you try to use it, you will receive the following message when you start your app:</p>
<blockquote>
<p>Warning: express.createServer() is deprecated, express applications no longer inherit from http.Server, please use:<br />
var express = require("express");</p>
<p>var app = express();</p>
</blockquote>
<p>Ok, no big deal, let's just do exactly what it says:</p>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kd">var</span> <span class="nx">express</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">express</span><span class="dl">'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">app</span> <span class="o">=</span> <span class="nx">express</span><span class="p">();</span></code></pre></figure>
<p><strong>Want to master Node.js?</strong> Try my highly-rated online course <a href="http://www.udemy.com/learn-nodejs-by-example/" title="Learn Node.js by Example">Learn Node.js by Example</a>. <a href="http://www.udemy.com/learn-nodejs-by-example/?couponCode=codehenge">Click here</a> to get 50% off on screencasts, interactive projects, and more!</p>
<p>However, you now have a very different object stored as 'app' then you did in Express 2.x. Before, app was derived from the HTTP server object type, which is what socket.io expects to be passed in its listen() method:</p>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kd">var</span> <span class="nx">io</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">socket.io</span><span class="dl">'</span><span class="p">).</span><span class="nx">listen</span><span class="p">(</span><span class="nx">app</span><span class="p">);</span></code></pre></figure>
<p>If you pass this new app object in, while no error will be thrown, the socket.io connections will not succeed. On launching your app, you will even get a helpful message:</p>
<blockquote>
<p>Socket.IO's `listen()` method expects an `http.Server` instance as its first parameter. Are you migrating from Express 2.x to 3.x?<br />
Which is a pretty good clue that you need to make some minor adjustments. Here's how to fix things.</p>
</blockquote>
<p>First, create an HTTP server object from your new Express 3.x app object:</p>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="nx">server</span> <span class="o">=</span> <span class="nx">http</span><span class="p">.</span><span class="nx">createServer</span><span class="p">(</span><span class="nx">app</span><span class="p">);</span></code></pre></figure>
<p>Now, pass this server object to socket.io instead:</p>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kd">var</span> <span class="nx">io</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">socket.io</span><span class="dl">'</span><span class="p">).</span><span class="nx">listen</span><span class="p">(</span><span class="nx">server</span><span class="p">);</span></code></pre></figure>
<p>Finally, you will need to invoke the server object to start listening on a port, rather than the app object, since the server object is what socket.io is linked with. This will work fine, but is against the standard Express 3.x convention:</p>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="nx">server</span><span class="p">.</span><span class="nx">listen</span><span class="p">(</span><span class="mi">8080</span><span class="p">);</span></code></pre></figure>
<p>Keep an eye out, as you this is a change you will have to make for the time being, especially when adding socket.io into existing Express 3.x apps.</p>
<p>Happy Noding!</p>{"login"=>"cacois", "email"=>"cacois@gmail.com", "display_name"=>"cacois", "first_name"=>"Constantine", "last_name"=>"Cois"}cacois@gmail.comExcited about version 3.x of Express, the Node.js web framework? It has some great new features, and is certainly worth migrating to. Many of my apps make use of Socket.io, one of my favorite Node modules, and in this case, and as of now (Socket.io v9.1) the migration isn't as smooth as it could be. Until socket.io catches up with the changes to Express, here's how you can use it with Express 3.x.JavaScript Performance Rundown, 20122012-08-05T01:23:27+00:002012-08-05T01:23:27+00:00http://www.codehenge.net/2012/08/javascript-performance-rundown-2012<p>Before giving a conference talk on Node.js earlier this summer, I ran some limited JavaScript benchmark tests, since I couldn't find any recent (this year) data posted. Inspired by <a href="http://ejohn.org/blog/javascript-performance-rundown/">John Resig's performance analysis</a> a few years ago, I decided to expand on that study by updating browsers and adding more benchmark suites to get a more comprehensive view of performance. I'll be presenting results for the 5 top desktop browsers:</p>
<ul>
<li>Google Chrome v20.0.1132.47 (V8 Engine) </li>
<li>Microsoft Internet Explorer v9.0.8112.16421 (Chakra Engine) </li>
<li>Firefox v14.0.1 (SpiderMonkey Engine) </li>
<li>Opera v12.00 (Carakan Engine) </li>
<li>Safari v5.1.7 (SquirrelFish Engine)</li>
</ul>
<p>Browser/engine versions are up to date at time of writing. Values reported are the mean of 10 consecutive runs with each browser. Tests were performed on a Intel Core i7 Macbook Pro with 8GB RAM, running Windows 7 Ultimate SP1.</p>
<p>First, let's check out the <a href="http://www.webkit.org/perf/sunspider/sunspider.html">SunSpider 0.9.1</a> JavaScript benchmark test:</p>
<p><figure><img src='assets/sunspider-2012-08-04.png' alt='' width='960' height='720'/><figcaption></a></p></figcaption></figure><p>Interestingly, the latest version of Microsoft's Chakra engine slightly edged out Chrome and its V8 engine in my test. Fear not, the result is not statistically significant. However, I do have to give credit to MS. For the capabilities tested by SunSpider, Chakra currently performs as well as V8. The remaining browsers went Opera, Firefox, Safari, from best to worst. These differences, though they appear small on the graph, were statistically significant (p < 0.01).</p>
<p>Moving on, I went to the <a href="http://v8.googlecode.com/svn/trunk/benchmarks/run.html">V8 JavaScript Benchmark Suite</a>. This is the suite used by Google internally to optimize and test the V8 engine, so expect V8 to perform well on this. Results:</p>
<p><figure><img src='assets/v8-benchmark-2012-08-04.png' alt='' width='960' height='720'/><figcaption></a></p></figcaption></figure><p>Chrome/V8 takes a solid lead here, with Firefox/SpiderMonkey a distant second, and everyone else trailing.</p>
<p>Finally, <a href="http://krakenbenchmark.mozilla.org/">Mozilla's Kraken benchmark suite</a>:</p>
<p><figure><img src='assets/kraken-benchmark-2012-08-04.png' alt='' width='960' height='720'/><figcaption></a></p></figcaption></figure><p>Again, Chrome/V8 and Firefox/SpiderMonkey in a league of their own, with Chrome slightly ahead. Results here were statistically significant (p < 0.01) as well.</p>
<p>So, for now, the V8 engine remains the champ (nothing to worry about here, Node.js users), though its nice to see an unexpected second wind from contenders like IE9 in some benchmarks. If I get time, I'll try to add a few more benchmark suites to round this out, though I did find some reported results for <a href="http://www.flickr.com/photos/26983785@N02/7158672499/">Chrome vs Firefox on Peacekeeper</a> that maintain V8's superiority.</p>{"login"=>"cacois", "email"=>"cacois@gmail.com", "display_name"=>"cacois", "first_name"=>"Constantine", "last_name"=>"Cois"}cacois@gmail.comBefore giving a conference talk on Node.js earlier this summer, I ran some limited JavaScript benchmark tests, since I couldn't find any recent (this year) data posted. Inspired by John Resig's performance analysis a few years ago, I decided to expand on that study by updating browsers and adding more benchmark suites to get a more comprehensive view of performance. I'll be presenting results for the 5 top desktop browsers...Best Resources to Learn Node.js2012-08-03T02:59:23+00:002012-08-03T02:59:23+00:00http://www.codehenge.net/2012/08/best-resources-to-learn-node-js<p><figure><img src='assets/nodejs.png' alt='Node.js' width='290' height='290'/><figcaption></a> <br</figcaption></figure>Node.js, a powerful JavaScript platform for building real-time, scalable applications, is gaining momentum among software developers. Node's event-driven, non-blocking approach to I/O fits perfectly with JavaScripts excellent event model, allowing complex applications to be developed intuitively, with less code. Node is also built on Google's V8 JavaScript engine, the superior runtime that gives the Chrome browser its blazing fast performance. Node is an up and coming technology, currently in use by powerhouses such as</p>
<p><a href="http://www.windowsazure.com/en-us/develop/nodejs/">Microsoft</a>, <a href="http://www.ebaytechblog.com/2011/11/30/announcing-ql-io/">Ebay</a>, <a href="http://blog.cloudfoundry.com/2012/05/24/cloud-foundry-supports-node-js-modules-with-npm/">VMWare</a>, and <a href="http://www.twitter.com">Twitter</a>.<br />
So, are you convinced? If you want to give Node.js a try, here's a list of my favorite Node tutorials, examples, and guides designed to help developers get up to speed on Node.js.</p>
<p></p>
<p></p>
<ol>
<li>
<p><strong><a href="http://www.nodebeginner.org" title="NodeBeginner.org" target="_blank">NodeBeginner.org</a></strong> NodeBeginner is an online book by Manuel Kiessling that aims to "get you started with developing applications with Node.js, teaching you everything you need to know about "advanced" JavaScript along the way". Filled with great explanations and a lot of Node examples, this book is a key resource to take a developer from Node.js beginner to programming useful applications. If you are anything like me, you'll also learn a few new things about JavaScript programming while you are at it! This is one of my favorites for those just getting started.</p>
</li>
<li>
<p><strong><a href="http://www.udemy.com/learn-nodejs-by-example/" title="Learn Node.js by Example" target="_blank">Learn Node.js by Example</a></strong> This is a course I developed for Node.js beginners, hosted at at Udemy.com. The course is project-based, guiding students to learn by developing example applications. Instruction is given mainly through screencasts, with a lot of time spent in code. I'm actively developing new content for it, and subscribers get access to all new content for no extra cost, even if the course goes up in price. <strong><a href="http://www.udemy.com/learn-nodejs-by-example/?couponCode=codehenge">Click here to get 50% off now!</a></strong></p>
</li>
<li>
<p><strong><a href="http://visionmedia.github.com/masteringnode/" title="Mastering Node" target="_blank">Mastering Node</a></strong> T.J. Holowaychuk, creater of pivotal Node.js tools such as Express and Jade, has an open source ebook called "Mastering Node" hosted at Github. The book, available in multiple formats, including html, mobl, epub, and pdf, is an excellent resource from one of the most knowledgeable Node.js programmers in the world. This is a Node guide you should definitely take a look at.</p>
</li>
<li>
<p><strong><a href="http://nodetuts.com" title="NodeTuts" target="_blank">NodeTuts.com</a></strong> Rails has Railscasts, and Node.js has NodeTuts http://nodetuts.com/. Pedro Teixeira has a large collection (30 and counting at the time of this writing!) of screencasts explaining various aspects of Node.js programming. His examples and explanations are practical, and extremely instructive. Highly recommended!</p>
</li>
<li>
<p><strong><a href="http://howtonode.org" title="HowToNode.org" target="_blank">HowToNode.org</a></strong> HowToNode is a community-supported blog created by Tim Caswell, the Node.js community manager at Cloud9 and core contributor to the Node.js open source project. The site contains a number of Node tutorials contributed by the Node.js community, on a variety of specific Node programming topics. This isn't where I'd start to learn Node.js as a beginner, but it is an excellent resource to be exposed to interesting techniques and topics once you have a basic understanding of Node.</p>
</li>
</ol>
<p>Now get out there and learn to Node!</p>
<p><strong>Get 50% off my Node.js course <a href="http://www.windowsazure.com/en-us/develop/nodejs/">here</a></strong></p>
<p>If you liked this article, help me out by sharing a 50% discount to my Node.js course here: <a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.udemy.com/learn-nodejs-by-example/?couponCode=codehenge" data-text="Sign up for Learn Node.js by Example now and get 50% off!" data-via="AaronCois" data-size="large" data-hashtags="nodejs">Tweet</a> Thanks!</p>
<p>You should follow me on Twitter here: <a href="https://twitter.com/AaronCois" class="twitter-follow-button" data-show-count="false" data-show-screen-name="false">Follow @AaronCois</a></p>{"login"=>"cacois", "email"=>"cacois@gmail.com", "display_name"=>"cacois", "first_name"=>"Constantine", "last_name"=>"Cois"}cacois@gmail.comIf you want to give Node.js a try, here's a list of my favorite Node tutorials, examples, and guides designed to help developers get up to speed on Node.js.Heading to OSCON 20122012-07-16T19:20:59+00:002012-07-16T19:20:59+00:00http://www.codehenge.net/2012/07/heading-to-oscon-2012<p>I'm excited to be heading out to OSCON 2012 in Portland, Oregon this week for a few days of open source fun! If anyone is at the conference, feel free to look me up. </p>{"login"=>"cacois", "email"=>"cacois@gmail.com", "display_name"=>"cacois", "first_name"=>"Constantine", "last_name"=>"Cois"}cacois@gmail.comI'm excited to be heading out to OSCON 2012 in Portland, Oregon this week for a few days of open source fun! If anyone is at the conference, feel free to look me up.