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