Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Deploying Your Existing StrongLoop Application to IBM Bluemix

DZone's Guide to

Deploying Your Existing StrongLoop Application to IBM Bluemix

Configure Bluemix to be capable of deploying StrongLoop applications that use MongoDB.

· Cloud Zone
Free Resource

Download this eBook outlining the critical components of success for SaaS companies - and the new rules you need to play by.  Brought to you in partnership with NuoDB.

If you are not familiar with StrongLoop, you may need to check my previous post:
http://www.technicaladvices.com/2015/09/12/build-and-customize-your-first-api-with-strongloop-in-less-than-7-minutes/

Deploying StrongLoop applications in Bluemix is tricky and requires you to do some modifications to your application. This post discusses the steps required to deploy a StrongLoop application that uses MongoDB to IBM Bluemix.

1. Create Your StrongLoop Application in Bluemix

Before deploying your local Strongloop application in IBM Bluemix, make sure to create a Bluemix “StrongLoop Arc” application from the Bluemix Boilerplates as shown below.
StrongLoop Arc App

Then create and bind a MongoDB Service to your created StrongLoop application in IBM Bluemix.
Bind MongoDB to Bluemix app

Then download the application starter code from "Start Coding" page, extract the zip file, and then get the manifest.yml file which is as follows in my case.


applications:
- path: .
  memory: 512M
  instances: 1
  domain: mybluemix.net
  name: iReport
  host: iReport
  disk_quota: 1024M
  services:
  - iReport-MongoDB

Copy the manifest.yml file to your local StrongLoop application’s directory.

2. Use VCAP_APP_HOST and VCAP_APP_PORT Inside Server.js File

In server.js file under the server directory, make sure to Use process.env.VCAP_APP_HOST and process.env.VCAP_APP_PORT as shown in the code below.


...
boot(app, __dirname, function(err) {
  if (err) throw err;

  //Use Bluemix host and port ...  
  var host = process.env.VCAP_APP_HOST || 'localhost';
  var port = process.env.VCAP_APP_PORT || 1337;

  app.set('host', host);
  app.set('port', port);  

  // start the server if `$ node server.js`
  if (require.main === module)
    app.start();
});

3. Use the “Start” Attribute in Package.json

If you do not use the "start" attribute in your package.json file, you may face the following exception when pushing your application to IBM Bluemix.


[App/0]  ERR npm ERR! Linux 3.19.0-25-generic
[App/0]  ERR npm ERR! argv "/home/vcap/app/vendor/node/bin/node" "/home/vcap/app/vendor/node/bin/npm" "start"
[App/0]  ERR npm ERR! node v0.12.7
[App/0]  ERR npm ERR! npm  v2.11.3
[App/0]  ERR npm ERR! missing script: start
[App/0]  ERR npm ERR! 
[App/0]  ERR npm ERR! If you need help, you may report this error at:
[App/0]  ERR npm ERR!     <https://github.com/npm/npm/issues>
[App/0]  ERR npm ERR! Please include the following file with any support request:
[App/0]  ERR npm ERR!     /home/vcap/app/npm-debug.log
[DEA/14] ERR Instance (index 0) failed to start accepting connections

This is my package.json file which uses the "start" attribute as follows.


{
  "name": "iReport-Services",
  "version": "1.0.0",
  "scripts": {
    "pretest": "jshint .",
    "start": "node server/server.js"
  },
  "dependencies": {
    "compression": "^1.0.3",
    "cors": "^2.5.2",
    "loopback": "^2.22.0",
    "loopback-boot": "^2.6.5",
    "loopback-component-explorer": "^2.1.0",
    "loopback-connector-mongodb": "^1.13.0",
    "loopback-datasource-juggler": "^2.39.0",
    "serve-favicon": "^2.0.1"
  },
  "devDependencies": {
    "jshint": "^2.5.6"
  },
  "repository": {
    "type": "",
    "url": ""
  },
  "description": "iReport-Services"
}

4. Update Datasources.json file Under the Application’s Server Directory

Under the server directory, there is a file called datasources.json which holds the application’s data source configurations, edit this file to point your application to the Bluemix MongoDB instead of your old MongoDB.

Below is my datasources.json file.


{
  ...
  ,
  "iReport-db": {
    "connector": "mongodb",
    "url": "mongodb://xxx"
  }
}

You can get the "url" attribute from the MongoDB Bluemix configuration as shown in the screenshot below.
Bluemix MongoDB

After doing the previous steps, you can finally, cf push [your local strongloop application] to IBM Bluemix.


Changing the configuration based upon NODE_ENV

It is also possible to change the configuration based upon NODE_ENV as suggested by Dennis Ashby (A StrongLoop expert).

The following better approach would be to take advantage of the StrongLoop Environment Specific configuration features: https://docs.strongloop.com/display/public/LB/Environment-specific+configuration

This will allow the application to change its configuration based upon the NODE_ENV value.  So for example, in Step 3, you can connect to different MongoDB instances based upon the NODE_ENV environment variable you have set in your Bluemix environment (dev, test, stage, prod, dr, etc).

So in Step 2, you can do the following:
1. In the server directory create a file called 'config.local.js'
2. The content of the file would be something like:

module.exports = {
  restApiRoot: '/api',
  host: process.env.VCAP_APP_HOST || 'localhost';
  port: process.env.VCAP_APP_PORT || 1337
};


Similarly for Step 3, you can do the following:
1. Modify the datasources.json to only have:

{
  ...
  ,
  "iReport-db": {
    "connector": "mongodb"
  }
} 


2. In the server directory create a file called 'datasources.XXX.js' where XXX is the value of NODE_ENV you are deploying.  Let's say XXX is 'dev'.  So, we would create a file 'datasources.dev.js'.

3. The content of the file would be something like:

module.exports = {
  iReport-db: {
     url: process.env.MONGODB_URL || "mongodb://xxx"
  }
};


These are all the steps you need to do in order to have your StrongLoop application up-and-running in Bluemix, Enjoy!

Learn how moving from a traditional, on-premises delivery model to a cloud-based, software-as-a-service (SaaS) strategy is a high-stakes, bet-the-company game for independent software vendors. Brought to you in partnership with NuoDB.

Topics:
bluemix ,mongodb ,strongloop

Published at DZone with permission of Hazem Saleh, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}