Product Documentation

c-treeDB API for Node.js

Previous Topic

Next Topic

Building Your Own Node.js NAV Project

Make sure you have Node.js and npm installed and executable from your command line.

Node.js changes slightly from version to version. We expect these directions to be similar for most versions; however, if you have difficulty, please contact FairCom.

  1. Create and enter a new folder, for example: test.
  2. Issue the following command inside the new empty folder:

    npm init --yes

    This will create a new package.json file with default values.

  3. Next, issue the following command:

    npm --save install path_to_your_ctdbsdk

    Replace path_to_your_ctdbsdk with the actual path to the FairCom DB navigational module for Node.js. This path is typically <FairCom-Folder>/drivers\nodejs.nav. You should not include the file name at the end of this path.

    This will create a node_modules folder with a link to the c-tree module and it changes package.json, adding a dependency to the module.

If you would like to get up and running with some example code, see the next page.

Note: It may be necessary to update to the latest version of npm. (For example, many Raspberry Pi Raspbian devices come pre-installed with an old version.) npm is easily updated as followed:
> sudo npm install -g npm@latest

Because of the dependency on c-tree libmtclient.so or mtclient.dll, you will need to ensure those are in your directory or path for proper loading. On Linux, use LD_LIBRARY_PATH to set the location of this shared library. Using bash, for example, the following assumes the library is in your current running location:

export LD_LIBRARY_PATH=<path-to-the-client-side-library>

Certain Linux platforms may also require exporting LD_PRELOAD to properly enable TLS (SSL) support. If you find a dlload error on execution, this may be a reason.

Locate your current openssl library and export this environment variable similar to the following example:

export LD_PRELOAD=/usr/lib64/libssl.so.1.0.2k

Both of these exports can also be included in a script used to execute your Node.js application.

On a newly installed Windows 10 machine, you may need to install windows-build-tools:

npm install --global --production windows-build-tools

The following commands should not be needed because this should be done transparently as a dependency of the module. If necessary, you may wish to install other Node.js packages, such as ffi-napi and ref:

npm install ffi-napi

npm install ref

To Learn More

To better understand the build process for Node.js, take a look at the Buildtutorials.bat file for Windows or the makefile for Linux. The key steps are as follows:

  1. Create a new project directory to work in, and move to that directory. Note: This step will vary by operating system.

    npm init -y

    npm --prefix . install <path to the drivers/nodejs.nav/ directory>

  2. Copy the mtclient shared library: mtclient.dll for Windows or libmtclient.so for Linux, from the drivers/ctree.drivers/lib/ directory to the new project directory.
  3. Copy all JavaScript files to the new project directory.

    node ctdb_tutorial1.js

Note: The FairCom driver for Node.js is designed for traditional, single-threaded Node.js applications. Each instance of Node.js should run one thread, but it can connect many times to the FairCom database for different purposes. Different instances of Node.js can safely connect to the same database and process data in parallel.

TOCIndex