How to add a Node.js Express route in a separate file

In this article I will show how you can manage your routes in a separate file from app.js. It also demonstrates more generally how adding modules to your applications works in node.js.


In this article I will create a simple route in a route.js file and reference it from my app.js. This will demonstrate how to keep the code separated and easier to manage.

The example application

This is a very simple express example with only two routes – the root of the app which says “Hi I am the root” and a second one which says “I am a new route”.

The initial app is a very basic app created using express.

// Startup Express App
var express = require('express');
var app = express();
var http = require('http').Server(app);


// handle HTTP GET request to the "/" URL
app.get('/', function(req, res) {
    res.write("Hi I am the root")


Which then produces the following simple page


New routes.js

I created the following file routes.js which will display a message when going to /marky

module.exports = function(app) {

    app.get('/marky', function(req, res) {
        res.write("I am a new route")

mobile.exports is node.js specific code which allows for code includes in this very manner. For more on this check out this article. Notice that (app) is passed to the function so that it properly scoped to the original code.

Back in the app.js we add a single line to require this new library and that’s it.

// Startup Express App
var express = require('express');
var app = express();
var http = require('http').Server(app);

//include other libraries
var routes = require('./routes')(app); //This is the extra line


// handle HTTP GET request to the "/" URL
app.get('/', function(req, res) {
    res.write("Hi I am the root")




More fundamentally than this simple example, this is the core of how node modules (including express) work. When you “require” express or http or any other module within your node application, this is how it is put together. Kinda cool 🙂

Creating a sample Hybrid Bluemix environment

In this article I will demonstrate how to create a sample Hybrid app running in IBM Bluemix but getting data from behind a company firewall.


A couple of years ago the prevailing message from vendors was “move to the cloud !!!”. The thing the vendors found though, was that the companies do not necessarily want to move their “data” to the cloud. It is either too complicated, expensive, unnecessary or they just do not flat out trust their data to the cloud. All that said though they are interested in the ability to securely expose their data to the outside world without exposing any of their internal systems. This has been achieved for years using a DMZ style firewall architecture which exposes only the web server but not the database server to the outside world.

In the Cloud world this concept is called a Hybrid model – cloud app, on premises data. In this article I want to show one way which IBM has approached this in Bluemix.


I wish I had listened to Ryan Baxter, last year at MWLUG 2014. I heard him talk about this concept and I serious thought to myself – who would want to do that. Being ahead of your time, happens to the best of us. Anyway you can see how Ryan set up his environment at that time using Cast Iron here. This is an excellent video and gives a nice overview of cast iron – that said, it is not the way I am going to do it and not the way IBM wants you to do it any more. So enjoy but come back….

I found most of the information I am going to write about today in this video…

(For more information on getting started with your first Bluemix application check out this NotesIn9 video)

So Bluemix

Within my Bluemix application I created a simple node application (xominoKnox) and then added the “Secure Gateway” Service.








I then created a Jazz Hub Git site and then cloned the repository locally (See this post for more information on that)

Creating the secure gateway

So the way that the gateway works is this:

  1. Create and configure the Bluemix end of the gateway
  2. Install the gateway code on the machine within the firewall
  3. Open the connection from inside the firewall
  4. Configure the connection to access data behind the firewall
  5. Use the connection

So let’s go through those steps one by one and explain what is going on.

1. Create and configure the Bluemix end of the gateway

Click on the Secure Gateway Service from within your Dashboard app view and you will see the configuration screen to create your first Gateway



Click Add Gateway and then you will be prompted to name your Gateway connection


Click Connect it and you will then be presented with the status screen – Not Connected



2. Install the gateway code on the machine within the firewall

The computer that you install the gateway on, inside your firewall, does not have to be the destination machine, it does however have to have access to the destination machine. Currently (April 2015) you will need to install a docker container on the machine and then inside of that the bluemix-secure-gateway can be installed. For those people without docker already, go here to get it installed.

NOTE FOR WINDOWS USERS: I had serious issues getting this installed due to the Oracle Virtual Box which has to be installed along with it. If you find that the Virtual box does not install – use this regedit hack to fix it.

This fixed it for me. Follow these step by step:

  1. Uninstall Virtualbox
  2. Uninstall Any Virtual Box Network Adaptors from Device Manager
  3. Go into the registry at: HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Network
  4. Change “MaxFilters” from 8 to 20 (decimal)”
  5. Reboot your PC
  6. Install VirtualBox 4.3.X (Run as Administrator)

3. Open the connection from inside the firewall

Once you have docker installed and running (Boot2Docker for windows), copy the commend below into your docker window and run it.



You will see the Connected message appear on your Secure Gateway dashboard and the tunnel connected message in the docker window.


The gateway is set up and a secure tunnel from Bluemix to my laptop (behind my office firewall) is set up.

To be clear at this point the outside world cannot access the Copper/xomino server running on my laptop from the outside world. It is just running on my laptop as normal within my development environment.

4. Configure the connection to access data behind the firewall

Next we have to create a destination (behind the firewall). Understand that the docker window is by default bridged and therefore does not know that it is running on my local computer ( IP address  I have given Bluemix is the IP address of the laptop on my network.




As you can see from the image above a Cloud Host and port has been assigned. If you do this a number of times you will see that the port changes.

You will also note that I chose not to use No TLS in the connection. This means that this is NOT PRODUCTION ready. We really need to secure this so that only my application can call that URL. More on that later – but for the  sake of this article/demo I am leaving it simple.

5. Use the connection

If we connect to the URL shown in the image above we can see a Domino server !!!



If we go to a specific page on that server we see this. Not much to look at I grant you, more on that in a later article.


But if we go to the gateway path – and add the “/xomino/ainx.nsf/testForm?readform” to the end of the URL – we get the exact same thing, from the exact same server, just displayed in a cloud app.


And that is very cool! Especially as it only took about 3 hours to figure this out 🙂


As we have seen in this article, it is relatively simple to set up a secure connection from a computer behind a firewall, and Bluemix. The example show it not yet fully secure though as anyone could call the URL and get web page from my server.

In a future article we will look at securing the connection and what else we are able to do with it.


The two sides of the incremental operator in JavaScript ++

I learned this last week, possibly highlighting my non-classical programming training. I have never come across this in all my years of JavaScript and apparently it is pervasive in other languages such as Java as well.

Incremental Operator ++

Many times I have seen or used the following method for incrementing an integer count

var marky = 0;

This increments the integer marky by 1. not rocket science. In the image below you can see that the two console.log entries always return the same value


What I learned yesterday was using the ++ after the variable name also increases the variable – but only AFTER the value has been evaluated…

From this image you can see that the marky++ value and the marky value are not the same. The console.log(marky++); returns the existing marky value and then increments it.


var marky = 0;

I have to ask myself “why would anyone do that?”. Why would I want to return a variable value and THEN increase it’s value, seems a little odd to me. I guess someone thought it was a good idea…..

Always makes me think “I wonder what else I don’t know? Probably best not think about that too much”….

The 300th Blog Post

I noticed I was getting close to this a week or so ago and I can honestly say I am very pleasantly surprised – I would never have thought I would still be going 3 years later. Yes that is almost an average of 100 posts a year – who would have thought I would have so much to say. Let alone anyone caring enough to read it.

So I just wanted to say thank you to everyone who has encouraged me along the way, and continues to encourage me. In particular I want to say thanks Dave Leedy (as always), without whom I don’t think I would have been able to get going as quickly.

I am blessed (well metaphorically anyway) to have met so many people and had such opportunities open up to me because of this blog. I’m not the only contributor, the readers and those who post comments are as well.

thank you all 🙂

Aborting a jQuery ajax request

In this article I will show how you can abort a jQuery ajax request, preventing your user experience from disappearing into space.


We have all done it – opened a page which runs an infinite loop consisting of some form of while(true). But no-one means to do that on purpose….That said there may be occasions where we are making requests to pages which we know are really really slow. Keeping a long connection open on the server restricts the number of open threads and potentially causes lockups. It is also a terrible user experience more importantly.

It might not be quite as relevant nowadays as it might have been to a Domino server say 10 years ago but still – pretty cool idea.

You can abort the ajax request with .abort()

Slowly loading XPage

I created a simple XPage with a before render response page which will take a long time to finish (as you can see below is ~30 seconds)

		for (var i=0; i<50000000; i++){
		return true}


Aborting the Ajax call

Aborting the call is relatively simple – by assigning a variable to the ajax call we can  then just .abort() it

var temp = $.ajax({
  url: ''
   console.log('hey what happened')


Running it manually we can see this



But a more practical solution is to set a timeout function to test the ajax value and abort if it is taking too long. How long that is, is entirely up to you……

function dontRunForever(url, time){
    var temp = $.ajax({
      url: url,
       console.log('hey what happened');
       console.log('All Done');
       temp = false

    setTimeout(function(){ //Using function() inside the setTimeout means the function is executed after the "time"
      } else {
       console.log('ajax call completed in time')
    }, time)


So when we run this with a timeout of 5 seconds, we see the abort() trigger


But when we use 50 seconds we see a successful completion and no abort message – and eventually the end of the Timeout success message


This is all well and good – BUT…..

Just because the ajax request is aborted – this does not mean the task stops running on the server – this whole concept improves the user experience and nothing else. This will not stop a never ending task running on the server – only the server settings will truly kill that.




When I found this and the fact that it has been around for over 4 years I was somewhat annoyed that I did not know about it earlier, but it is pretty cool still all the same 🙂