Full-Stack JavaScript

End-to-End JavaScript: Node.js And MongoDB

Why Full-Stack Javascript (FJS)?

  • The majority of our devs are experienced with JavaScript, but may not be as familar with server side languages like PHP.
  • FJS has the potential to offer those same devs a simpler transition into backend development.
  • The ability to use JS end to end, including Front-End (AngularJS, Backbone), Back-End (expressjs), Templating (handlebars), Testing (mocha, chai), Package Management (bower, npm), and Task Management (grunt).
  • Brings balance to the Force.

How does it work?

1. Client request to Nginx

Nginx sits in front of Node and handes all http requests from the client. This ensures Nginx is used for things it excels at like serving static assets, caching, and SSL.

^ Back to TopNext >>

2. Is a static asset

If nginx determines that the incoming request is a static asset it will be served directly back to client without touching node.

e.g. HTML, images, css, javascript etc..

^ Back to TopNext >>

3. Not a static asset

If the request is not a static asset nginx will then pass it on to Node to process.

^ Back to TopNext >>

4. Mongo Queries

Node will query MongoDB for any required data.

^ Back to TopNext >>

5. Mongo Response

Node will process the JavaScript object response from Mongo.

^ Back to TopNext >>

6. Send response to client

Node will process the request and send the appropriate response back to the client.

^ Back to Top

Getting Started - Local Environment

For local development we will leverage a tool called StackStrap. This allows us to rapidly install and manage a Node environment that runs in a VM on the dev's machine.

Each StackStrap project has a template that acts as a blueprint for what needs to be installed for your environment. In our case we have a Node project template that installs the following:

CentOS, git, nginx, nvm, mongoDB, avahi

More about the environment

  • As well as serving static assests nginx is used to handle SSL and caching.
  • Node Version Manager (nvm) is used to install the latest version of Node.js.
  • We install expressjs web application framework for Node.js using Node Package Manager (npm).
  • We install Forever a Node module that ensures the Node application runs continuously (i.e. forever).

Vagrant and SALT

StackStrap uses Vagrant and Salt, but marries the two together by providing an easier way to configure them. There is no requirement to be an expert with these tools, but it is still recommended that you familiarize yourself with them.

  • Vagrant does an amazing job of automating the task of bringing up virtual machines for development.
  • Salt installs software packages, starts or restarts services, or puts configuration files in place and watches them for changes.

Installing StackStrap

Before you install StackStrap ensure that you have met the below dependencies first.

Once the dependencies have been installed you are ready to install StackStrap from github:

							$ sudo easy_install stackstrap

Add Node.js template

Once you have successfully installed StackStrap you will need to add the Node.js template that has already been created for you.

To add the the template to StackStrap run:

You will only run this command ONCE. New node projects will use this same template.

							$ stackstrap template add django https://github.com/freesurface/stackstrap-django.git

Creating a new Node.js project

Once you have successfully added the Node.js template you are ready to create a new project.

  1. Create a new Project:
    								$ stackstrap create mynewproject django
  2. Change into the new folder:
    								$ cd mynewproject
  3. Vagrant Up:
    								$ vagrant up --provision

Test Local Environment

If all has gone well Vagrant will have started up a VM running your new project's Node.js environment.

When you create a new project with StackStrap it will set up a local domain name for you. This domain is assembled using your project name and system user name.

To test that it's working visit your project domain in a browser.

Be sure to replace {myprojectname} and {username} with your information.


Moving to Production

Close collaboration with the hosting provider will be necessary to ensure that production mirrors your local Node.js environment.

  • Server should have a minimum of two cores.
    (1 for nginx and 1 for Node)
  • git, nginx, mongoDB, and nvm installed
  • Provide your nginx config file.

Useful Commands

Vagrant Command Line Interface

							$ vagrant ssh

							$ vagrant up --provision

							$ vagrant destroy

							$ vagrant halt

If you are having issues connecting to your StackStrap domain try:

							$ dscacheutil -flushcache

Useful Links

Next Steps

This guide is just a primer for setting up a Full-Stack Javascript development environment. For a deeper dive you can find a detailed install guide on github.