Bitnami NodeJS for Google Cloud Platform

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

How to connect your Node.js application to an Oracle Database?

In order to connect your Node.js application to an Oracle Database, you need to install the following packages.

  • libaio
  • Oracle Instant Client (and SDK)
  • oracledb Node.js module

Step 1: Install libaio

  • Execute the following command:

    $ sudo apt-get install libaio1
    

Step 2: Install Oracle Instant Client and Oracle Instant Client SDK

  • Download both instantclient-basic-linux.x64.zip and instantclient-sdk-linux.x64.zip. The version will depend on the Oracle Database you have running. For instance, for Oracle Database 12c you would download instantclient-basic-linux.x64-12.1.0.2.0.zip and instantclient-sdk-linux.x64-12.1.0.2.0.zip (we will use them in the rest of the examples).

  • Uncompress instantclient-basic-linux.x64.zip by executing the following command:

     $ unzip instantclient-basic-linux.x64-12.1.0.2.0.zip
    
  • Copy the extracted contents to /opt/bitnami/common/lib:

     $ sudo cp -r instantclient_12/* /opt/bitnami/common/lib
    
  • Execute the following command:

     $ sudo ln -s /opt/bitnami/common/lib/libclntsh.so.12.1 /opt/bitnami/common/lib/libclntsh.so
    
  • Extract instantclient-sdk-linux.x64-12.1.0.2.0.zip:

     $ unzip instantclient-sdk-linux.x64-12.1.0.2.0.zip
    
  • Copy the contents of the sdk subfolder to /opt/bitnami/common/include:

     $ sudo cp -r instantclient_12/sdk/include/* /opt/bitnami/common/include
    

Step 3: Install OracleDB Node.js module

  • Before installing, set the following environment variables:

     $   export OCI_LIB_DIR=/opt/bitnami/common/lib
     $   export OCI_INC_DIR=/opt/bitnami/common/include
    
  • In your Node.js application folder, execute the following command:

     $   npm install oracledb
    

Step 4: Test the connection

In order to test that the plugin was installed correctly, execute one of the examples detailed in the OracleDB Node.js module documentation.

  • Download this example from the OracleDB Node.js module website.

  • In the same folder where connect.js was downloaded, create the file dbconfig.js with the following contents (remembering to replace the DATABASE_USER, DATABASE_PASSWORD and DATABASE_CONNECTION_STRING placeholders with the correct values):

     module.exports = {
          user          : "DATABASE_USER",
          password      : "DATABASE_PASSWORD",
          connectString : "DATABASE_CONNECTION_STRING"
     };
    

    For example:

     module.exports = {
        user          : "SYSTEM",
        password      : "mypassword",
        connectString : "127.0.0.1:1521/PDB1.oracleuser.oraclecloud.internal" 
     };
    
  • Run the testing program:

    $ node example.js
    

If everything was correct, you should see the message "Connection was successful!"

How to create a custom Node.js application?

Many users run a Bitnami stack as a development environment for their own projects (as opposed to running third-party applications). To create a custom Node.js application from scratch, follow the steps below. These steps assume that your application will live in the /opt/bitnami/apps/myapp/ directory:

  • Run the following commands to create the directories:

    $ sudo mkdir -p /opt/bitnami/apps/myapp
    $ sudo mkdir /opt/bitnami/apps/myapp/conf
    $ sudo mkdir /opt/bitnami/apps/myapp/htdocs
    
  • Create and edit the /opt/bitnami/apps/myapp/conf/httpd-prefix.conf file and add the line below to it:

    Include "/opt/bitnami/apps/myapp/conf/httpd-app.conf"
    
  • Create and edit the /opt/bitnami/apps/myapp/conf/httpd-app.conf file and add the content below to it. This is the main configuration file for your application, so modify it further depending on your application's requirements.

    ProxyPass / http://127.0.0.1:3000/
    ProxyPassReverse / http://127.0.0.1:3000/
    
    NOTE: 3000 is the default port for the Express server. If you have customized your application to use a different port, change it here as well.
  • Once you have created the files and directories above, add the following line to the end of the main Apache configuration file at /opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf, as shown below:

    Include "/opt/bitnami/apps/myapp/conf/httpd-prefix.conf"
    
  • Restart the Apache server:

    $ sudo /opt/bitnami/ctlscript.sh restart apache
    
  • Start the Express server:

    $ cd /opt/bitnami/apps/myapp/htdocs
    $ ./bin/www
    

    Alternatively, use the following command to start the server and keep it running even after your server session ends. Replace FILE with the correct filename for your application.

    $ forever start FILE.js
    

Although your application is now available, you may not be able to access it immediately. This is because the Express server runs on port 3000 by default, and Bitnami stacks on some platforms have this port closed for security reasons. To access the application, you will need to create an SSH tunnel to the port.

How to create a project using Express?

Express is a high-performance Web development framework for Node.js inspired by Sinatra.

  • Installing it is as simple as executing the commands below:

     $ sudo npm -g install express
     $ sudo npm -g install express-generator
    
  • Create a skeleton project by executing these commands. This will produce a new application in the projects/sample-application directory:

     $ mkdir -p projects
     $ cd projects
     $ express sample-application
    
  • Install dependencies using npm:

     $ cd sample-application
     $ npm install
    

This will download and install the required dependencies into a node_modules directory under your project folder.

How to install Node.js applications?

NOTE: To avoid a conflict between two or more Node.js applications attempting to use the same port, modify the application settings as needed to use a different port for each application.

To install a Node.js application, first ensure that all dependences are satisfied. Many applications will require Node.js and MongoDB, but others may require Redis or MySQL. To check available services, run this command:

$ sudo /opt/bitnami/ctlscript.sh status

To install an application in the example/ directory, follow these steps:

  • Create the directory:

     $ sudo mkdir /opt/bitnami/apps/example
    
  • Move the application files to that directory and follow the official installation steps.

How to install modules?

Node.js comes with the npm package manager. Using it, you can install, uninstall and search for Node.js modules.

npm installs modules in two different scopes: local and global.

Local Installation

The local installation is recommended, as the size of the packages is usually small. It installs the packages in a node_modules subdirectory of the current working directory, usually a project folder. This ensures all the required dependencies are installed and won't break when installing other packages in a different project or locally.

To install modules locally, you just have to execute this command:

$ npm install PACKAGE

For example, to install MySQL, you would execute:

$ npm install mysql

Global Installation

In other cases (typically when the module installs a command line tool), it is more convenient to install the packages globally. In these cases, the command varies slightly:

$ sudo npm -g install PACKAGE

For example, to install Express, you would execute:

$ sudo npm -g install express

This will make the Express command-line available in all projects.

We recommend installing packages in both local and global modes, so that you can share installed commands and still protect the dependencies of your project.

How to list the installed modules?

To list the modules installed locally in a project, enter the project directory and execute the npm list command, as shown in the example below.

$ cd sample-application
$ npm list
  application-name@0.0.1 /home/bitnami/projects/sample-application
+-- express@2.5.8
| +-- connect@1.8.7
| | +-- formidable@1.0.9
| +-- mime@1.2.4
| +-- mkdirp@0.3.0
| +-- qs@0.4.2
+-- jade@0.26.1
  +-- commander@0.5.2
  +-- mkdirp@0.3.0

You can also list the globally installed packages by entering the project directory and executing the npm -g list command, as shown below:

$ cd sample-application
$ npm -g list
  /opt/bitnami/nodejs/lib
+-- coffee-script@1.3.3
+-- colors@0.6.0-1
...
+-- express@2.5.9
| +-- connect@1.8.7
| +-- qs@0.4.2
| +-- cliff@0.1.7
...
| | +-- colors@0.6.0-1
+-- jade@0.26.0
| +-- commander@0.5.2
| +-- mkdirp@0.3.0
+-- node-dev@0.2.3
+-- npm@1.1.21
| +-- abbrev@1.0.3
| +-- node-gyp@0.4.1
| | +-- ansi@0.0.4
| +-- uid-number@0.0.3
| +-- which@1.0.5
+-- uglify-js@1.2.6

How to start the Express web application?

To start your application server, you just have to execute the following command, inside your application directory:

$ npm start

Alternatively, use the following command to start the server and keep it running even after your server session ends. Replace FILE with the correct filename for your application.

$ forever start FILE.js

If you are using a recent Express version (greater than 4.0), you should use the following command instead:

$ ./bin/www

Port 3000 is the default port used by Express when creating the template. This means that you may receive an error similar to the below when starting the server:

Error: listen EADDRINUSE
 at errnoException (net.js:670:11)
 at Array.0 (net.js:771:26)
 at EventEmitter._tickCallback (node.js:190:38)

This means that the port is already in use. Edit the app.js script and change the line containing app.listen() to use a different port number, as below. Then, start the application again.

app.listen(4000, function(){

How to upgrade npm?

You can upgrade npm to the latest version with the following command:

$ npm -g install npm
google

Bitnami Documentation