/restful-api

A basic but complete implementation of a RESTful API

Primary LanguageJavaScriptGNU General Public License v3.0GPL-3.0

restful-api

A basic but complete implementation of a RESTful API

Installation Process

Development Environment

It's the set of processes and programming tools used to create the program or software product. The term may sometimes also imply the physical environment. This installation takes course in an Ubuntu 15.10 x64 laptop computer.

  • Memory: 5.7 GiB
  • Processor: Intel® Core™ i5-3337U CPU @ 1.80GHz × 4
  • Graphics: GeForce GT 630M/PCIe/SSE2
  • OS Type: 64-bit
  • Disk: 116.3 GB

Note: these are a lot higher than the minimun requirements.

Uninstall what may cause conflict

nodejs and npm packages are way out of date in the ubuntu repository, so lets uninstall them just in case, so we won't have any conflict in the future.

Uninstall Commands

Let's uninstall nodejs and npm:

:~$ sudo apt-get -y --purge remove nodejs npm
:~$ sudo apt-get autoremove -y

Installation

About the text editor or IDE, i use SublimeText 3, available in sublime. For NodeJS follow the instructions in NodeJS Installation. And for MongoDb follow the instructions in MongoDB Installation.

Check versions
:~$ nodejs --version
-> v4.3.1
:~$ npm --version
-> 3.8.0

Run Project

First, go to the projetc's folder, then run:

:~$ npm install
:~$ gulp apidoc
:~$ gulp nodemon

And you'll have a server running in http://localhost:3000 waiting for you! (If you clicked and the server was running, the existing routes documentation should be shown).

Testing Environment

It's function is to deliver a a software that is validated, stable and usable to execute the test scenarios or replicate bugs. This installation takes course in an Ubuntu 14.04.3 x64 server instance located in DigitalOcean cloud hosting. This guide is for a node.js API server, using express.js and mongodb as database server.

MongoDB Installation

Import the public key

Import the public key used by the package management system The Ubuntu package management tools (i.e. dpkg and apt) ensure package consistency and authenticity by requiring that distributors sign packages with GPG keys.

:~$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927 
Create a list file for MongoDB

Create the /etc/apt/sources.list.d/mongodb-org-3.2.list list file using the command appropriate for your version of Ubuntu:

:~$ sudo nano /etc/apt/sources.list.d/mongodb-org-3.2.list
Edit the file

Write this in it:

deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse
Reload local package database

Issue the following command to reload the local package database:

:~$ sudo apt-get update
Install the MongoDB packages

You can install the latest stable version of MongoDB. Issue the following command:

:~$ sudo apt-get install -y mongodb-org
Check Version

Run this command:

:~$ mongo --version
-> MongoDB shell version: 3.2.3
Possible errors

One of the possible errors that might be found is with the global initialization.

Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and/or LC_* environment variables are set correctly.
How to fix it?

For temporary fix, just run:

:~$ sudo export LC_ALL=C

or

:~$ sudo locale-gen "en_US.UTF-8"

For a more permanent approach, edit the /etc/environment file and add this in it:

LC_ALL=C
LANG=en_US.UTF-8

NodeJS Installation

Debian and Ubuntu based Linux distributions

Node.js is available from the NodeSource Debian and Ubuntu binary distributions repository (formerly Chris Lea's Launchpad PPA). Support for this repository, along with its scripts, can be found on GitHub at nodesource/distributions.

:~$ sudo curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
:~$ sudo apt-get install -y nodejs 
:~$ sudo npm update -g # run this until no response from the terminal
Install build tools (optional)

To compile and install native addons from npm you may also need to install build tools:

:~$ sudo apt-get install -y build-essential
Install global packages
:~$ sudo npm install -g apidoc flightplan forever gulp jshint mocha nodemon

Nginx Installation

Install Nginx

We can install Nginx easily because the Ubuntu team provides an Nginx package in its default repositories.

:~$ sudo apt-get update
:~$ sudo apt-get install -y nginx
Config Server

Create the file sudo nano /etc/nginx/conf.d/servers.conf and add the configuration for our server. Our server will be running in the 3000 port, so we need to configure our nginx server as a proxy.

:~$ sudo nano /etc/nginx/conf.d/servers.conf

And add our configuration:

server {
    listen 80;
    server_name our.domain.com;
    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 3000;
        proxy_send_timeout 3000;
        proxy_read_timeout 3000;
        proxy_buffers 4 32k;
        client_max_body_size 8m;
        client_body_buffer_size 128k;
    }
}

Finally restart the nginx server:

:~$ sudo service nginx restart

Testing Deployment

Server Side

First of all we have to configure our server, SSH protocol, SSH key, a local user and a remote user. Then we can use flightplan:

:~$ fly setup:testing # just the first time
-> # Check there are no errors
:~$ fly build:testing # do this once you are sure there are no problems in the app
-> # Check there are no errors
:~$ fly deploy:testing
-> # Check there are no errors