Prerequisites
Our examples make use of NodeJS and MongoDB, so be sure the have them installed and have mongo running on your machine.
We recommend downloading NodeJS, MongoDB, Nginx and NPM via Homebrew (Mac) or APT-GET (Ubuntu)
We'll use Node to write our code, Mongo as our database, Nginx as our web server, load balancer and for upstream and finally NPM as our nodejs package manager.
Installation
Now that we have all technologies we're going to use, let's go ahead and download the Utilities & Examples and go through them step by step.
The Utilities contains CLI commands to bootstrap some database information so that the examples can run.
Downloading the Utilities & Examples has never been easier, simply use NPM.
# create a directory to install the components in it > mkdir -p /opt/soajs/node_modules > cd /opt/soajs/node_modules # install the components > npm install soajs.utilities
Hosts File
Add the following entry to your hosts file:
> vim /etc/hosts # add the below line in your hosts file 127.0.0.1 test-api.mydomain.com
Nginx
Go to your Nginx configuration directory:
- Mac: /usr/local/etc/nginx
- Ubuntu: /etc/nginx
Create a new file called upstream.conf in folder conf.d
# upstream all traffic arriving from port 80 to port 4000 upstream soajs.controller { server 127.0.0.1:4000; }
Create a new file called api.conf in folder sites-enabled
# create new server test-api.mydomain.com that listens on port 80 and redirects to soajs.controller located in upstream.conf server { listen 80; server_name test-api.mydomain.com; client_max_body_size 100m; location / { proxy_pass http://soajs.controller; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_set_header Connection ""; } }
Once these files are created, restart nginx.
# stop nginx sudo nginx -s stop # start nginx sudo nginx
This will instruct your web server NGINX to accept calls made to test-api.mydomain.com on port 80 and upstream them to port 4000 thus invoking the controller on IP 127.0.0.1
Utilities Parameters
Parameter | Key | Mandatory | Description | Default Values |
---|---|---|---|---|
File | f | yes | The name of the file to be loaded | - |
Host | h | no | The host machine (IP Address) | 127.0.0.1 |
Port Number | p | no | The port number on which MongoDB is listening | 27017 |
Username | u | no | The username required for the database authentication | - |
Password | w | no | The password required for the database authentication | - |
Authentication Database | a | no | The name of the database to which the user is authenticated | - |
Prefix | i | no | the prefix of the db if one was set in the installation | - |
Patch | x | no | parameter to denote whether or not the command is to patch the database (can only be true or false) | false |
NOTE: If you set up Mongo with authentication then username, password, and authentication database must be provided.