OpenShift, express.js and spark.js — A bit farther

After work­ing with git, Open­Shift, node.js and all of the oth­er stuff from my last post to final­ly have a sim­ple “Hel­lo World”, I took the next step of actu­al­ly putting more than just a page with hel­lo world in my new appli­ca­tion.

  1. First I decid­ed to go with express.js as my start­ing point. I had already installed express.js to use, but they also have an express gen­er­a­tor that builds out a stan­dard appli­ca­tion with fold­er struc­ture and oth­er sup­port­ing items. You can find the details and install stuff here: http://expressjs.com/starter/generator.html
    1. If you installed express glob­al­ly, you might also need to install it in your app node_modules also after your run the gen­er­a­tor. If you try npm install after the gen­er­a­tor and you get a ton of errors cd into myapp/node_modules and npm install express into this loca­tion. Good info here: http://blog.nodejs.org/2011/03/23/npm-1–0-global-vs-local-installation .
  2. Now that I have a skele­ton, I need­ed to put it in my git repos­i­to­ry, not in its appli­ca­tion fold­er name like it was cre­at­ed. This ensures it goes into the cor­rect loca­tion when I com­mit to Open­Shift. Make sure you leave the .git and .open­shift in your repos­i­to­ry!
  3. Since I now have a new start­ing loca­tion, it was server.js as the Open­Shift stan­dard; I need­ed to update my package.json so the app starts where I need it to.

package.json

"name": "spark",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"main": "./bin/www",

"dependencies": {
"express": "~4.9.0",
"body-parser": "~1.8.1",
"cookie-parser": "~1.3.3",
"morgan": "~1.3.0",
"serve-favicon": "~2.1.3",
"debug": "~2.0.0",
"jade": "~1.6.0"
}
}

4.  Also, the stan­dard www file in the express build needs to be updat­ed so it pulls in the envi­ron­men­tal vari­ables from Open­Shift.

./bin/www

#!/usr/bin/env node
var debug = require('debug')('spark');
var app = require('../app');
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.set('host', process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1");
var server = app.listen(app.get('port'), app.get('host'), function() {
debug('Express server listening on port ' + server.address().port);
});

The last two steps are well doc­u­ment­ed here: https://blog.openshift.com/run-your-nodejs-projects-on-openshift-in-two-simple-steps

Once you com­mit every­thing you should now see your com­plet­ed Wel­come to Express page and now you can start build­ing out your app!

This will be my start­ing point for inter­act­ing with my Spark.IO devices and work­ing with spark.js. Hope­ful­ly I will have some work­ing exam­ples soon!