Updated 2014/03/20: Added disableBuild
to prevent restarts.
Remember those days where JavaScript were those snippets of buggy code sitting in HTML pages? Well, those days are over.. JavaScript is hot! It’s what I write 100% native mobile apps in, most of Titanium tools themselves are written in it and with Ti.Next (Titanium 4.0) even the compiler and the API will be all JavaScript. Thanks to NodeJS, JavaScript quickly became a full-stack solution.
Geddy
So, after having done a CLI in NodeJS, I felt for this new project I’m doing I might as well try doing a back-end in it. After spending some time with plain Express and Sails, I ended up using the Geddy framework for this. The framework has been out there since the early days of NodeJS (2010) and features great documentation and (community) support.
Geddy + OpenShift
I hosted my previous back-ends on a private PHP server. Since Appcelerator has good connections with Red Hat’s OpenShift, I decided to use their NodeJS server. I didn’t find an official guide on how to use Geddy on OpenShift, but it was relatively easy to figure out so here you are:
Set up OpenShift
- Create a free account at Redhat OpenShift
- Add an application using the NodeJS 0.10 cartridge.
- Follow the instructions to clone the repository, but don’t commit anything just yet.
- On your application’s dashboard, also add the MongoDB 2.2 cartridge to your app.
Set up Geddy
On your local machine:
- Install Geddy globally:
$ [sudo] npm install -g geddy
- Change to the folder under which you want Geddy to create another folder with your app. Make sure Geddy will not overwrite the cloned repository!
- Generate an Geddy app:
$ geddy gen app <name>
- Copy the contents of the Geddy app to the cloned repository:
$ cp -a <name>/ <clone>/
- Delete the Geddy app:
$ rm -rf <name>
Configure Geddy
In the repository folder:
- Generate a secret for sessions and so on:
$ geddy gen secret
- Open
config/secrets.json
and make the following change:
1"secret": "<%= process.env.OPENSHIFT_SECRET_TOKEN ? process.env.OPENSHIFT_SECRET_TOKEN : 'YoUrSeCrEt' %>" - Open
.gitignore
and remove the following line:
1config/secrets.json - Open
package.json
and make the following changes:
1234567891011"dependencies": {"geddy": "geddy/geddy","mongodb": "1.3.x","mongodb-wrapper": "1.0.3"},"engines": {"node": ">= 0.6.0","npm": ">=1.0.0"},"private": true,"main": "server.js"disableBuild
later on. Mongo is globally available, but Geddy requires it local. We need mongodb-wrapper for storing sessions in a Mongo DB. The engines configuration is the default for a NodeJS cartridge. - Open
server.js
and replace the contents with:
123456789#!/bin/env nodevar geddy = require('geddy');geddy.start({port: process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || '3000',hostname: process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1',environment: 'production'});geddy
CLI command to start the server, so the fallbacks to local ports and hostname are not required, but there so you can test it locally. - Open
config/production.js
and make the following changes:
123456789101112131415161718192021222324..db: {mongo: {username: process.env.OPENSHIFT_MONGODB_DB_USERNAME,password: process.env.OPENSHIFT_MONGODB_DB_PASSWORD,host: process.env.OPENSHIFT_MONGODB_DB_HOST,port: process.env.OPENSHIFT_MONGODB_DB_PORT,dbname: process.env.OPENSHIFT_APP_NAME}},sessions: {store: 'mongodb',server: {user: process.env.OPENSHIFT_MONGODB_DB_USERNAME,password: process.env.OPENSHIFT_MONGODB_DB_PASSWORD,host: process.env.OPENSHIFT_MONGODB_DB_HOST,port: process.env.OPENSHIFT_MONGODB_DB_PORT,db: process.env.OPENSHIFT_APP_NAME},key: 'did',expiry: 14 * 24 * 60 * 60},disableBuild: true..disableBuild
is a new option I’ve added that will prevent Geddy from generating new helpers and models in the public directory. Because OpenShift uses supervisor, this would trigger an endless loop of restarts.
Deploy to OpenShift
- Add files:
$ git add .
- Commit changes:
$ git commit -am "Added Geddy"
- Push to OpenShift:
$ git push
- Open the application in your browser.