Install And Configure Custom Module in Node project

Modules in Node.js ecosystem is the building block for reusing your own code while utilizing someones work. It becomes a little bit tricky if you don’t want install module from the center NPM system(https://npmjs.org/) but would like to get it from github or package your own. In this example, we have project that service as Rest Client for making REST calls on some other application. Instead, building Rest Client we want to reuse one from https://github.com/dchester/node-rest-client

Step-1: Clone REST client

To grab the Rest client from github:

git clone https://github.com/dchester/node-rest-client.git rest-client

Step-2: Create Node project

a) Create Directory (i.e mkdir someProject)
b) Create Node project configurations by running:

npm init

This will create the package.json file you need for configure project including dependencies
c) Specify the dependency for ‘rest-client’ module in package.json file:

...
     },
    "dependencies": {
    "rest-client": "*"
     },
    "repository": "",
...

Step – 3: Install dependencies

To install dependencies run:

npm install

This will look into the package.json and load, configure modules. In our case, module name ‘rest-client’.

Step – 3: Create interface script

Before run it, lets create and interface for each REST methods – GET, PUT, DELETE, POST. Here is an example of GET method that in our interface will be named as ‘show’ method:

ar Client = require('rest-client');

var minApp = new Client({ host: 'localhost:8010/restapi', logLevel: 'OFF' });

exports.show = function(domain, id) {
 minApp.get({
 url: '/' + domain,
 params: { id: id },
 success: function (data) {
 console.log('response received...');
 }
 });
}
exports.update = function(name) {
..
exports.delete = function(name) {
...
exports.save = function(name) {
...

Side Node: we running this client against grails application that has domain resources with Rest Api. So, each domain resource has a controller by the name of domain name with 4 actions – show, update, delete, save together implementing REST Api. That is way you see we are passing in domain name to our request.

Step-4: Run it

While you can run it from the script, i suggest running from the Node command line or also called Read-Eval-Print-Loop(REPL) interface. This way you continuously can make any REST requests by passing parameters you like. So, to start REPL interface run ‘node’ on command line:

○  node
> var client = require('./restApiRequest.js');
undefined
> client.show('review',2)
undefined
> client.show('review',3)
undefined
...

Here, we start the interactive interface, load our REST client interface, and then keep calling any of the REST methods that generate the requests

Tracing Requests

To make sure actual requests are made, we use ‘tcpflow’ utility. If you don’t have to installed, then on Mac run the following:

sudo port install tcpflow
sudo port activate tcpflow

With ‘tcpFlow’ installed, start listening on some port as follows:

sudo tcpflow -c -i lo0 port 8010

Now if you make request you should see something like:

127.000.000.001.59465-127.000.000.001.08010: GET /restapi/review?id=3 HTTP/1.1
host: localhost:8010
Connection: keep-alive

Here we used our RESt client and called show method with id 3 (i.e. client.show(‘review’,3))

Udemy Tutorial

I haven’t used a lot of Udemy Tutorial but it appears a good resource for learning Node.js and other technologies.

Useful Links

Install Node from Source Ubuntu

In this post, we log the steps to install Node.js on Ubuntu from source (the alternatives are many to install from package manger with one good source of information here )

Step 1 – Get the source from Nodejs.com

Go to to http://nodejs.org and copy link of the source tar file of your interest. Afterwards, open console and move, unpack, extract the file as follows:

wget https://nodejs.org/download/release/v0.10.6/node-v0.10.6.tar.gz
tar xvzf node-v0.10.6.tar.gz
cd node-v0.10.6

Make sure your grab the right one source package not the binary archive packages, otherwise you will miss necessary files such as .configure to install it in next step

Step 2 – Compile and Install Node JS

From the console, run the following commands:

cd node-v0.10.6
./configure
make
make install

Firs, the configuration is created created in line 1. Then, the ‘make’ command compiles Node.js after. Afterwards, the ‘make install’ does the installation including copying files accordingly

Step 3 – Test the Installation

Check the version:

node -v

Lets create file test.js that contains the following line:

console.log('hello world');

Afterwards, run this node.js script:

node test.js

This should display the ‘hello world’ text

NOTE: Make sure you check Node Version Manager tool that will automatically grab Node.js and let you install version you wish.

Uninstall Nodejs

First, lest find out which executable used for nodejs

which node

This will return something like /path/bin/node.
Next, go to the path dir:

cd /path

Once in the /path dir, to remove:

rm -r bin/node bin/node-waf include/node lib/node lib/pkgconfig/nodejs.pc share/man/man1/node.1

This will remove Nodejs but not npm. If you install npm again into a custom path that starts off empty, then you can see what it adds and then you will be able to make a list for npm similar to the above list I made for node.