Created
July 29, 2015 00:20
-
-
Save krhoyt/d60271a44c94068ef6c3 to your computer and use it in GitHub Desktop.
IBM Mobile Cloud Services Boilerplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Libraries | |
var express = require( 'express' ); | |
var ibmbluemix = require( 'ibmbluemix' ) | |
var ibmdata = require( 'ibmdata' ); | |
var winston = require( 'winston' ); | |
// Constants | |
var LOG_PATH = '../logs/winston.log'; | |
// Bluemix | |
ibmbluemix.initialize( { | |
applicationId: '_YOUR_APPLICATION_ID_', | |
applicationRoute: '_YOUR_APPLICATION_ROUTE_', | |
applicationSecret: '_YOUR_APPLICATION_SECRET_' | |
} ); | |
// Environment | |
var ibmconfig = ibmbluemix.getConfig(); | |
// Logging | |
var ibmlogger = ibmbluemix.getLogger( { | |
transports: [ | |
new( winston.transports.Console )(), | |
new( winston.transports.File )( { | |
filename: LOG_PATH | |
} ) | |
], | |
filename: true, | |
methodname: true, | |
linenumber: true | |
} ); | |
// Express | |
var app = express(); | |
// Request forwarding | |
// Pass along services on the request | |
app.use( function( req, res, next ) { | |
ibmlogger.info( 'Forwarding ...' ); | |
req.data = ibmdata.initializeService( req ); | |
next(); | |
} ); | |
// Static content | |
app.use( ibmconfig.getContextRoot() + '/public', express.static( 'public' ) ); | |
// Straight hello world | |
app.get( ibmconfig.getContextRoot() + '/hello', function( req, res ) { | |
var hello = null; | |
// Log | |
ibmlogger.info( 'Say hello to: World' ); | |
// Build record | |
hello = req.data.Object.ofType( 'Hello', { | |
name: 'World' | |
} ); | |
// Save record | |
hello.save().then( function( success ) { | |
// Success | |
res.send( 'Hello World (from Node)!' ); | |
}, function( error ) { | |
// Error | |
res.send( error ); | |
} ); | |
} ); | |
// Hello world (with name) | |
app.get( ibmconfig.getContextRoot() + '/hello/:name', function( req, res ) { | |
var hello = null; | |
// Uppercase first letter | |
req.params.name = | |
req.params.name.substring( 0, 1 ).toUpperCase() + | |
req.params.name.substring( 1, req.params.name.length ); | |
// Log | |
ibmlogger.info( 'Say hello to: ' + req.params.name ); | |
// Build record | |
hello = req.data.Object.ofType( 'Hello', { | |
name: req.params.name | |
} ); | |
// Save record | |
hello.save().then( function( success ) { | |
// Success | |
// The argument has many properties | |
// Take a look using: req.json( success ); | |
res.send( 'Hello ' + req.params.name + ' (from Node)!' ); | |
}, function( error ) { | |
res.send( error ); | |
} ); | |
} ); | |
// Start server | |
app.listen( ibmconfig.getPort() ); | |
ibmlogger.info( 'Server started on port: ' + ibmconfig.getPort() ); | |
/* | |
* http://${appHostName}.mybluemix.net/${appHostName}/v1/apps/${applicationId}/public | |
* http://android-weather.mybluemix.net/android-weather/v1/apps/0b6a4a12-1ef4-43af-999b-2a50bea45628/public | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
applications: | |
- services: | |
- Android Weather-MAS | |
- Android Weather-Push | |
- Android Weather-MobileData | |
- Android Weather-Mobile Quality Assurance | |
disk_quota: 1024M | |
host: android-weather | |
name: Android Weather | |
command: node app.js | |
path: . | |
domain: mybluemix.net | |
instances: 1 | |
memory: 128M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "Weather", | |
"version": "0.0.1", | |
"description": "Simple weather lookup.", | |
"main": "app.js", | |
"scripts": { | |
"start": "node app.js" | |
}, | |
"dependencies": { | |
"express": "^4.13.1", | |
"ibmbluemix": "^1.0.0", | |
"ibmdata": "^1.0.0", | |
"request": "^2.58.0", | |
"winston": "^1.0.1" | |
}, | |
"repository": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment