Skip to content

Instantly share code, notes, and snippets.

@cianclarke
Created December 14, 2016 08:26
Show Gist options
  • Save cianclarke/cdf9ae957461a3a4a5cd0ee123bb1b13 to your computer and use it in GitHub Desktop.
Save cianclarke/cdf9ae957461a3a4a5cd0ee123bb1b13 to your computer and use it in GitHub Desktop.
var mbaasApi = require('fh-mbaas-api');
var fh = mbaasApi;
var express = require('express');
var mbaasExpress = mbaasApi.mbaasExpress();
var cors = require('cors');
// list the endpoints which you want to make securable here
var securableEndpoints;
securableEndpoints = ['/hello'];
var app = express();
// Enable CORS for all requests
app.use(cors());
// Note: the order which we add middleware to Express here is important!
app.use('/sys', mbaasExpress.sys(securableEndpoints));
app.use('/mbaas', mbaasExpress.mbaas);
// allow serving of static files from the public directory
app.use(express.static(__dirname + '/public'));
// Note: important that this is added just before your own Routes
app.use(mbaasExpress.fhmiddleware());
app.use('/hello', require('./lib/hello.js')());
app.use('/cache', function(req, res){
var operation = req.query.operation;
var key = req.query.key;
var value = req.query.value;
if (['load', 'save'].indexOf(operation)===-1){
return res.status(405).end('Not a supported operation');
}
fh.cache({
act : operation,
key : key,
value : value
}, function(err, cacheResult){
if (err){
return res.status(500).json(err);
}
return res.json(cacheResult);
});
});
app.use('/db', function(req, res){
var operation = req.query.operation;
var type = req.query.type;
var value = req.query.value;
if (['create', 'list'].indexOf(operation)===-1){
return res.status(405).end('Not a supported operation');
}
fh.db({
type : type,
act : operation,
fields : { name : value }
}, function(err, dbResult){
if (err){
return res.status(500).json(err);
}
return res.json(dbResult);
});
});
// Important that this is last!
app.use(mbaasExpress.errorHandler());
var port = process.env.FH_PORT || process.env.OPENSHIFT_NODEJS_PORT || 8001;
var host = process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
app.listen(port, host, function() {
console.log("App started at: " + new Date() + " on port: " + port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment