Skip to content

Instantly share code, notes, and snippets.

@cianclarke
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cianclarke/272fae631ab34ac38161 to your computer and use it in GitHub Desktop.
Save cianclarke/272fae631ab34ac38161 to your computer and use it in GitHub Desktop.
iBeacons cloud code for returning beacon descriptions from databrowser
var mbaas = require('fh-mbaas-express');
var express = require('express');
// Securable endpoints: list the endpoints which you want to make securable here
var securableEndpoints = ['hello'];
var app = express();
// Note: the order which we add middleware to Express here is important!
app.use('/sys', mbaas.sys(securableEndpoints));
app.use('/mbaas', mbaas.mbaas);
// Note: important that this is added just before your own Routes
app.use(mbaas.fhmiddleware());
app.use('/hello', require('./lib/hello.js')());
app.use('/beacons', require('./beacons.js'));
// You can define custom URL handlers here, like this one:
app.use('/', function(req, res){
res.end('Your Cloud App is Running');
});
// Important that this is last!
app.use(mbaas.errorHandler());
var port = process.env.FH_PORT || process.env.VCAP_APP_PORT || 8001;
var server = app.listen(port, function(){
console.log("App started at: " + new Date() + " on port: " + port);
});
/*
NB I'm including this file in my application.js folder by calling :
app.use('/beacons', require('./beacons.js'));
*/
var $fh = require('fh-mbaas-api');
module.exports = function(req, res, next){
$fh.db({
act : 'list',
type : 'beacons'
}, function(err, data){
if (err){
return res.error(err);
}
var beacons = {};
data.list.forEach(function(b){
beacons[b.fields.minor] = b.fields;
});
return res.json(beacons);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment