Creating a Node.js & PostGIS App on OpenShift

来源:互联网 时间:1970-01-01

This is the first part of what I plan to be a series of setting up an app built on PostGIS, Node.js, Leaflet.js, and D3.js to visualize the routes a climber has done (ticks) and those that they want to do (todos). Specifically the idea is to show where those routes are on the earth and some interactivity to help better understand details about a climbers past ticks and future todos. For this post we will ignore the map and just focus on getting Node and Postgis on OpenShift.

To make sure everyone is up to speed I figure I’ll go over some questions that might come up on this topic.

1. What is Node.js?

Answer: A framework running JavaScript from server to client. It’s a fairly disruptive concept but as I’m learning has some nice advantages that we wont get into here.

2. What isPostGIS?

Answer: PostGISis the amazingspatial bindings to PostgreSQLthat, when used together, make the best damn spatial database that exists today.

3. What are Leaflet.js and D3.js?

Answer: Leaflet is a really nice mapping JavaScript library for putting dynamic maps on the web. D3.js is a really powerful visualization library that excels at making customized web visualizations. Both are damn awesome.

4. What is OpenShift?

Answer: OpenShiftis Red Hat’s P latform A s A S ervice (PAAS) solution for hosting web applications. It makes lifereally easy because OpenShift manages all the infrastructure around your app while also incorporating a bunch of really handy tools. It’s also really cheap for a simple non-resource needy app. OpenShift is also damn awesome.

5. Why am I writing about this topic?

Answer: There just wasn’t enough on the internet for me to easily have my simple questions answered so I thought I would try to add my experience. There are a couple of write-ups on the internet coveringthis topic but the primary OpenShift blogwas broken at the time.

What you would need if you where trying to set this up as well A free (or paid if ya like) OpenShift account. To set up OpenShift client tools. Node.js installed on your computer. PostgreSQL and PostGIS installed on your computer (if you want to test locally). Git installed on your computer. Some JavaScript skillz (yes, with a ‘z’). 2 Ways to setup an initial environment Using OpenShift’s instant app functionality is probably the easiest and fastest way to get started. Of course, you can also convert any Node.js app to OpenShift with a couple easy modifications.

Lets cover both methods just for fun.

Method #1 – OpenShift’s Handy Instant App

1. After setting up an account with OpenShift create a new Node.js app following the basic instructions. There are lots of resources for doing this online by using either OpenShift’s command line tools, the OpenShift web interface, or both. Hereis an official doc from OpenShift on the topic.

NOTE: Leave the“Source Code” field blank if using the web interface. See Method #2 below for more details.

2. Now that you have an app created you can add a PostgreSQL database to it… This is amazingly easy. Simply go to your apps main page on the OpenShift website and clickthe “Add PostreSQL x.x” button in the databases section below your apps cartage section. This will ask if you want to add the PostgreSQL cartage to which you will click “yes”.

Take note of your PostgreSQL’s credentials. Those are important.

3. To get PostGIS installed is easier than it used to be but you still have to SSH into your app. You can find the SSH command on your apps home page of the OpenShift website. This is what my SSH command looks like:


ssh [email protected] [email protected]

Once you are SSH’d into your app you have access to PSQL which is the primary terminal based language for interacting with PostgreSQL. Type ‘psql’ in the terminal. The prompt should change a bit to something like this ‘APPNAME=#’. That’s good. That means your terminal is in psql mode. Now simply type:


The terminal should stall for a couple seconds and then output ‘CREATE EXTENSION’.

Congratulations! You just setup a new Node.js app with a PostGIS database!

4. Now that you’vecreated your first OpenShift Nodeapp you can clone the Git repo that OpenShift requires you to use as a code repository. This is what your clone command looks like if typed in the terminal.

git clone ssh://[email protected]/~/git/THENAMEOFYOURAPP.git/ git clonessh://[email protected]/~/git/THENAMEOFYOURAPP.git/

Why clone a repo you might ask?

Answer: OpenShift relies on the wildly popular Git as a version control system for your applications source code. They do some amazing things to make your life easier like managing your apps infrastructure, allowing simple integration of build tools (like Jenkins), and simply allowing you to redeploy an app with a ‘git push’ to your remote OpenShift repo. I love that I rarely have to SSH into my app to pull new source or other menial tasks.

This repo is fully configured to run on openshift. All you have to do now is make any desired code changes and push the changes to OpenShift via Git. This means deploying your changes to your app is as simple as:

git push origin master git pushoriginmaster When running this command you should see all kinds of messages indicating fun messageslike your app and database cartridges are restarting. Method #2 – Adding an existing Node.js app to OpenShift

1. Step one for this method is very similar to method #1 except that you must use the option to add an existing Git repo to the application during the setup process. I havn’t done this via terminal but on the web interface you enter a URL to your Git repository in the “Source Code” field. You can optionally specify a branch.

2. Step 2, 3, and 4 are the same as in method #1 so…Follow those steps to add a PostgreSQL database, add the PostGIS extension, and clone the OpenShift Git repo.

3. Now, in my experience there was really only a couple changes to make to get my Node.js app to work using this method. They primarily have to do with the fact that OpenShift will not modify existing files or create certain configuration files for you. There is some documentationon this but I will outline my steps here for the sake of clarity.

Make sure there is a package.json file in the root of the project. From what I can tellitis standard Node to have this so this step is basically a gimme. However, my default node configuration was different so I changed the ‘scripts.start’ and ‘main’ options to reference the correct files.You can see my whole package.json file below. Add some OpenShift specific settings to the top of your main app configuration file (mine is called server.js). This is needed because OpenShift uses environment variables in your app environment to set the port and domain for your app. The example code below will use port 8080 and a localhost domain ( if the OpenShift environment variables don’t exist. This is useful for not having to change code between your dev and production environments. You can see my example below.

server.js OpenShift specific configuration example


var express = require('express');var path = require('path');var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080;var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || ''; app.set('port', process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 8080);app.set('ip', process.env.OPENSHIFT_NODEJS_IP || "");app.listen(server_port, server_ip_address, function () { console.log( "Listening on " + server_ip_address + ", server_port " + server_port )});... varexpress=require('express');varpath=require('path'); varserver_port=process.env.OPENSHIFT_NODEJS_PORT||8080;varserver_ip_address=process.env.OPENSHIFT_NODEJS_IP||''; app.set('port',process.env.OPENSHIFT_NODEJS_PORT||process.env.PORT||8080);app.set('ip',process.env.OPENSHIFT_NODEJS_IP||""); app.listen(server_port,server_ip_address,function(){ console.log("Listening on "+server_ip_address+", server_port "+server_port)}); ...

package.json example:



{ "name": "APPNAME", "version": "0.0.0", "private": true, "main": "server.js", "scripts": { "start": "node server.js" }, "dependencies": { "body-parser": "~1.13.2", "cookie-parser": "~1.3.5", "debug": "~2.2.0", "express": "~4.13.1", "hjs": "~0.0.6", "morgan": "~1.6.1", "serve-favicon": "~2.3.0" }} { "name":"APPNAME", "version":"0.0.0", "private":true, "main":"server.js", "scripts":{ "start":"node server.js" }, "dependencies":{ "body-parser":"~1.13.2", "cookie-parser":"~1.3.5", "debug":"~2.2.0", "express":"~4.13.1", "hjs":"~0.0.6", "morgan":"~1.6.1", "serve-favicon":"~2.3.0" }}

That’s all folks! I think next I will cover the basics of using your new database but this is enough for now. I hope this helped some of you stumble a little less than I did.