Node.js Getting Started

Posted on Friday, April 10, 2015




What is node.js? 

The very short answer, it’s a way to run JavaScript outside of the browser (ex. server side).  

The better answer… watch the first 2 minutes of this video https://youtu.be/pU9Q6oiQNd0 [1]  (also watch the rest of the video, it’s a very nice Hello World+ tutorial)



Watch the video, its worth the time.



Installing Node.js and npm (Node Packet Manager)


The current, stable, version of Node.js is 0.12.0 released on 2/6/2015 (as of 3/30/2015)
The current stable release of npm is 2.6.0 released on 2/13/2015




Installing on Windows 7 Cygwin


I jump back and forth between windows, linux, and OSX every day.   Without Cygin Windows would be unbearable.   I'm going to get node working with Cygwin.

According to this page https://github.com/joyent/node/wiki/Installation#building-on-cygwin [7] they stopped supporting cywin, they do have a deploy for build 0.4.12 which seems too far out of date to use, however can it be compiled and deployed by hand?





       Github install


(I was uncessful in getting this to work)

Use node's github repo located at https://github.com/joyent/node [2]

Download the git repo.


> git clone https://github.com/joyent/node.git


Checkout the v0.12.0-release


  > cd node
  > git checkout v0.12.0-release



Now build it by hand


  > ./configure
  > make
  > sudo make install




After make I got this error



Looking around a bit it seems this may be a very hard, so for now I am going to give up my dream of running it natively in Cygwin and just do the Windows MSI install and have cygwin use that.





      Normal boring download and install (win 7)…






Download the Windows installer, I am on a 64-bit machine so I am going to download the 64-bit version.



After it downloads run it.


Click Next








Click Next.




Click Next.




Click Install




Wait for it to install




Click Finish.

Start a new cygwin terminal and check the versions


  > node -v
  > npm -v






Looks good.









Installing on OS X


Use brew to install it.


  > brew install node



Check the versions


  > node -v
  > npm -v




OK, not the latest version, can I get 0.12 installed via Brew?







    Github install  (OS X)


Use node's github repo located at https://github.com/joyent/node [2]

Download the git repo.


> git clone git@github.com:joyent/node.git


Checkout the v0.12.0-release


  > cd node
  > git checkout v0.12.0-release



Now build it by hand


  > ./configure
  > make
  > sudo make install




Check the versions


  > node -v
  > npm -v





That worked.







Installing on Ubuntu 14.04



  > sudo apt-get update
  > sudo apt-get install node
  > sudo apt-get install npm


This install nodejs at /usr/bin/nodejs, I would rather have it at /usr/bin/node so I need to make a link.


  > sudo ln -s /usr/bin/nodejs /usr/bin/node




Check the versions


  > node -v
  > npm -v




OK these are older versions, how do I install the new ones in Ubuntu 14.04?






    Github install (Linux)


Use node's github repo located at https://github.com/joyent/node [2]

Download the git repo.


> git clone git@github.com:joyent/node.git


Checkout the v0.12.0-release


  > cd node
  > git checkout v0.12.0-release




Now build it by hand


  > ./configure
  > make
  > sudo make install




Check the versions


  > node -v
  > npm -v




OK that did not get me npm 2.6.0, but I don't think I need it per-se so I am going to leave it as is and call it good.










Node Hello World


OK, now for some simple hello world programming.

You can run node.js interactively.


  > node


Now you are in an interactive node shell





Run a few commands


> console.log("Hello World");
> var a = 1;
> var b = a*3;
> console.log(b + " is a number");




To get out of this interactive mode press ctrl+c twice







Run Javascript from a file


Create a module.


  > vi my_module.js


Place the following in it


console.log("Hello World");
var a = 2;
var b = a*3;
console.log("b is '" + b + "'");






Save the file and now run it with the following command.


  > node my_module.js








What is npm?


NPM is our node packet manager.  It's a tool that allows us to easily bring in other libraries and manage any dependencies your code may have.

List the current modules.  Documentation on npm list can be found at  https://docs.npmjs.com/cli/ls [3]


  > npm ls




Nothing is installed yet.




I am going to make a project folder and start importing a module or two.


  > cd
  > mkdir node_code
  > cd node_code


To search for packages you can go here https://www.npmjs.com/ [4] and search for a package.




I ran a search for express and this is what I came up with.






Install express


Express is a minimal web framework.

To install it run the following command


  > npm install express




It installs a few things

If I run


  > npm ls


I get back more interesting results now.





I installed these packaged locally in this project, they are located in the node_modules folder








The package.json file


There is a better way to handle your project's dependencies, the package.json file. 

To create this file run the following command.


  > npm init


This walks you through the creation of the package.json file with a few prompts.







You can see here it already found our dependencies we had set up before.

What about if/when I want to add another module?

If I run


  > npm install jquery


It will install the module in the node_code folder, but it won't update the package.json file.




To have it auto update package.json use the -S flag.

Run


  > npm install jquery -S




Now it updates correctly.

Why is this helpful?



Wipe the node_modules folder and run "npm install"


  > rm –r node_modules
  > npm install


This loads all the library dependencies from package.json





Now that is done, I am going to write a very simple express program and run it.


  > vi app.js


And put the following in it.  (tweaked from  http://expressjs.com/starter/hello-world.html [5])


var express = require('express');
var app = express();

//Set Environment
app.set('port', 3000);

app.get('/', function (req, res) {
    res.
send('Hello World!');
   
console.log("Responded to request " + req.hostname);
});

app.listen(app.get('port'), function () {
   
console.log("Express Server Started Listening on port " app.get('port'));
});




Save it and run it


  > node app.js





Then open up a web browser to view it  http://localhost:3000/







I don't want to go any deeper into Express in this document, I will save that for a later post.  But for now I have a very basic node express server running.








Poking around


Now that I have node and npm installed I want to poke around a bit to see what it can do.

One thing I want to do is interact with the linux shell.  I want to run a simple command and get some data back.

Looks like nodes usese the child_process to handle this https://nodejs.org/api/child_process.html#child_process_child_process [6]

Create a new node file


  > vi process.js


Place the following into it.


var cp = require('child_process');

cp.exec('echo "Hello World"', function(error, stdout, stderr) {
   
console.log(stdout);
});




Save it and run it


  > node process.js







Update the code to the following and run it again


var cp = require('child_process');

cp.exec("du -s . | awk '{print $1}'",
                      function(error, stdout, stderr) {
   
var diskUsage = Number(stdout);

   
console.log(diskUsage + " KiB");
   
console.log((diskUsage / 1024).toFixed(1) + " MiB");
});



Save it and run it


  > node process.js








References

[1]        What is Node.js Exactly? - a beginners introduction to Nodejs
                        https://youtu.be/pU9Q6oiQNd0
                Accessed 3/2015
[2]        Node.js github repo
                        https://github.com/joyent/node
                Accessed 3/2015
[3]        npm list (documentation)
                        https://docs.npmjs.com/cli/ls
                Accessed 3/2015
[4]        npm web site.
                        https://www.npmjs.com/
                Accessed 3/2015
[5]        Express Hello World
                        http://expressjs.com/starter/hello-world.html
                Accessed 3/2015
[6]        Child Process Documentation
                        https://nodejs.org/api/child_process.html#child_process_child_process
                Accessed 3/2015
[7]        Building on Cygwin
                        https://github.com/joyent/node/wiki/Installation#building-on-cygwin
                Accessed 3/2015


2 comments: