Thursday, January 29, 2015

Installing Node.js on Ubuntu 14.10

Environment


  • node.js 0.10.33
  • Ubuntu 14.10
    • running on VirtualBox 4.3.20


Introduction


Node.js (aka "node") is javascript on the server.  Node enables Javascript to talk to the Operating System, exposes APIs for the file system, network streams, and so on.  Node is enabled via the Chrome V8 Javascript engine.


Installing Node.js

Node can be installed in four simple steps on Ubuntu:

  1. sudo add-apt-repository ppa:chris-lea/node.js
  2. sudo apt-get update
  3. sudo apt-get install nodejs
  4. sudo npm install npm -g

Do not install nodejs using the apt package manager. This will install an older version. Use a PPA (personal package archive) to get a more up to date version.

Once the installation has been performed, determine which versions were installed on the terminal:
craig@U14BASEBIG01:~$ node --version && npm --version  
v0.10.33  
2.3.0  

Hello, World (Web)

The best way to verify the installation is by creating a simple Hello, World program:
var http = require('http');  
http.createServer(function (req, res) {  
     res.writeHead(200, {'Content-Type': 'text/plain'});  
     res.end('Hello World!\n');  
}).listen(1337, '127.0.0.1');  
console.log('Server running at localhost:1337');  

Save this file (e.g. "hello-world.js") and run it on the terminal by typing:
craig@U14BASEBIG01:~$ node hello-world.js   
Server running at localhost:1337  

Then open up your web browser and verify that the server is indeed running, and serving up files:



Hello, World (Console Only)

Although node.js might be best known for serving up files over a browser, it's not necessary to use it solely in this manner.

In this example, I'm going to create two javascript files.  One file will reference a module, and invoke a method, and log it to the console.

call-mod.js

var myMod = require('./mymodule');
myMod.sayHello();

mymodule.js

exports.sayHello = function() {
     console.log("hello there!");
     var greet1 = "greetings #1";
     greet2 = "greetings #2";
}

On the terminal, I can invoke call-mod.js like this:
craig@U14BASEBIG01:~$ node call-module.js
hello there!

So the advantage here is I am coding in Javascript, without the need to run this in the context of a larger application stack (eg. Tomcat) etc.

NB: Note the scope of variables in Javascript.  The second variable (greet2) is accessible to call-mod.js (should we wish to invoke it).  The absence of the "var" modifier makes this variable global.  I include this as a reminder, not as a recommended design pattern.


References


  1. How to install node.js on Ubuntu 14.04
    1. Skip the first section and focus on the PPA installation
  2. How to install node.js and npm on Ubuntu 14.04
    1. I followed the advice in this blog for this article.
  3. [YouTube] Node.js and Express 101
    1. Great introductory advice for node.js 

2 comments:

  1. Thanks Admin, I just begin my career in sever side web application development for that I complete the Node JS training but I wish to explore more in that, truly your blog provide some useful information and I would like to share your blog in my friends circle so keep sharing.
    Regards,
    Node JS training in chennai

    ReplyDelete
  2. I feel happy and learn more about this subject. keep sharing your information regularly for my future reference. This content creates in me new hope and new inspiration. Thanks for sharing an article like this.
    Node JS Online training
    Node JS training in Hyderabad

    ReplyDelete