Getting started with node.js

15.05.2012 22:10Comments

What is node.js?

node.js is a development platform based on Javascript created by Ryan Dahl and currently maintained by the cloud provider Joyent. The interesting thing about node.js is that it runs in a single thread, and attacks concurrency by trying to block the CPU as little as possible (if at all). In it’s most simplistic form, node.js provides a REPL interface for the command line: repl node.js happens to be very good at running web servers, apart from being very simple to do so, the non-blocking philosophy makes it a very good allied of concurrency, since it’s not limited to the amount of threads that your process can run. Here’s how a hello world web server looks like:

var http = require('http');
http.createServer(function (request, response) {
	response.writeHead(200, { 'Content-Type': 'text/plain' });
	response.end('Hello World\n');
console.log('Server running at');
And you could could run this very easily if you save it to a file like this:
node index.js
Another very interesting thing about writing web applications in node, is that you keep the client side and server side languages consistent. If you add a json-document based NoSQL database like mongo to the mix, you have yourself a complete javascript/json environment for developing websites.

Getting started in windows

If you want to get started with windows, you can download the installer from the node.js website. At the moment of writing this post, the version 0.6.18 was only 3MB. After you install, you will find the nodejs installation under you ‘program files(86)’ folder. You can open your cmd or powershell command line, and you will already have the node and npm command at your disposal. There are a number of cloud services that will host your node.js applications out there like: Getting up and running in Azure turns out to be pretty simple with the latest SDK they released. You can get the SDK using the Web Platform Installer searching for node: wpi And you will get a set of powershell commandlets for creating node webroles, and even deploying to azure from the command line. And last but not least, if you want to host your node.js apps in IIS, you can install IISNode for IIS. From the IISNode, here are the benefits:
  • Process management
  • Side by side with other content types
  • Scalability on multi-core servers
  • Integrated debugging
  • Auto-update
  • Access to logs over HTTP
  • Minimal changes to node.js application code
  • Integrated management experience
  • Other IIS benefits. Port sharing, security, URL rewriting, compression, caching, logging

Getting started in linux

You can also go to the node.js website and download the installer, but there’s another cool way of getting node in your linux environment called “nvm”. Nvm is a sh script that will download, build and manage the different versions of node that you might want to use in your environment. For getting nvm, all you have to do is go to the nvm github website, and copy the script into some file in you computer (for example ~/nvm/, and then either execute or add this to your bash profile file: . ~/nvm/ Once you have nvm, you can do the following things: Install a specific version: nvm install v0.6.18 Start using a specific version: nvm use v0.6.18

…and you are ready to node!

Writing javascript for node is no different than writing javascript for any other platform (including browsers), so you can really any text editor that supports the javascript syntax. Lately, I have been using Sublime Text 2 thanks to my friend Jose Romaniello and it’s awesome and I can use it in both Windows and Linux! As for resources for starting to use node these seem to be the best out there:

comments powered by Disqus