1 Introduction
The popularity of JavaScript applications has been skyrocketing in the last few years, with Node.js definitely facilitating this growth. If we look at modulecounts.com we will see that there are more Node packages in the wild than in the Ruby world. In addition, Node packages are growing faster than Ruby, Python, and Java combined.Data from modulecounts.com (collected by scraping the relevant websites once a day) In this article we are going to take a look at the most important aspects of Node so you can get on track with it and start building applications right away.
1.1 What's making Node more popular than Rails and other alternatives?
Node markets itself as an asynchronous, event-driven framework built on top of Chrome's JavaScript engine and designed for creating scalable network applications. It's basically JavaScript plus a bunch of C/C++ under the hood for things like interacting with the filesystem, starting up HTTP or TCP servers and so on.Node is single-threaded and uses a concurrency model based on an event loop. It is non-blocking, so it doesn't make the program wait, but instead it registers a callback and lets the program continue. This means it can handle concurrent operations without multiple threads of execution, so it can scale pretty well.
In a sequential language such as PHP, in order to get the HTML content of a page you would do the following:
$response = file_get_contents("http://example.com");
print_r($response);
In Node, you register some callbacks instead:var http = require('http');
http.request({ hostname: 'example.com' }, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log(chunk);
});
}).end();
There are two big differences between the two implementations:- Node allows you to perform other tasks while waiting to be notified when the response is available.
- The Node application is not buffering data into memory, but instead it's outputing it chunk-by-chunk.
In Node, all of the libraries have been designed from the ground up to be non-blocking, but the same cannot be said for the others.
1.2 npm, the official Node package manager
Node owes a big part of its success to npm, the package manager that comes bundled with it. There are a lot of great things about npm:- It installs application dependencies locally, not globally.
- It handles multiple versions of the same module at the same time.
- You can specify tarballs or git repositories as dependencies.
- It's really easy to publish your own module to the npm registry.
- It's useful for creating CLI utilities that others can install (with
npm) and use right away.
2 Installing Node.js and NPM
There are native installers for Node on Windows and OS X, as well as the possibility of installing it via a package manager. However sometimes you will want to test your code with different Node versions, and that's where NVM (Node version manager) comes in.With NVM you can have multiple versions of Node installed on your system and switch between them easily. In the next few lines we are going to see how to install NVM on an Ubuntu system.
First, we have to make sure our system has a C++ compiler:
$ sudo apt-get update
$ sudo apt-get install build-essential libssl-dev
After that we can copy-paste the one-line installer for NVM into the terminal:$ curl https://raw.githubusercontent.com/creationix/nvm/v0.13.1/install.sh | bash
At this moment NVM should be properly installed, so we will logout and login to verify that:
$ nvm
If there's no error when typing in the
nvm command, that means that everything's alright. Now we can move on to actually installing Node and npm.
$ nvm install v0.10.31
The output should look like this:
$ nvm install v0.10.31
################################################################## 100.0%
Now using node v0.10.31
Both
node and npm should be available in the terminal now:
$ node -v && npm -v
v0.10.31
1.4.23
There is one last thing we need to do so we can always use this version of Node when we login the next time: making this version the default one.
$ nvm alias default 0.10.31
We can install other Node versions just like we did before and switch between them with the
nvm use command:
$ nvm install v0.8.10
$ nvm use v0.8.10
In case you are not sure what versions you have installed on your system just type
nvm list. That will show you the full list of versions and also the current and default versions, such as the following:$ nvm list
v0.6.3 v0.6.12 v0.6.14 v0.6.19 v0.7.7 v0.7.8 v0.7.9 v0.8.6 v0.8.11 v0.10.3 v0.10.12 v0.10.15 v0.10.21 v0.10.24 v0.11.9 current: v0.10.24 default -> v0.10.24