Created
September 24, 2015 15:00
-
-
Save cianclarke/5c016539e0fc06a3363f to your computer and use it in GitHub Desktop.
Service Proxy - set env var EXAMPLESERVICE, then replace cloud app's application.js with this
This file contains hidden or 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
| var serviceId = process.env.EXAMPLESERVICE || "yourServiceIdHere"; | |
| var fh = require('fh-mbaas-api'); | |
| var express = require('express'); | |
| var mbaasExpress = fh.mbaasExpress(); | |
| var cors = require('cors'); | |
| var bodyParser = require('body-parser'); | |
| // list the endpoints which you want to make securable here | |
| var securableEndpoints; | |
| // fhlint-begin: securable-endpoints | |
| securableEndpoints = ['/hello']; | |
| // fhlint-end | |
| var app = express(); | |
| // Enable CORS for all requests | |
| app.use(cors()); | |
| app.use(bodyParser.json()); | |
| // 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()); | |
| // fhlint-begin: custom-routes | |
| app.all('/*', function(req,res) { | |
| return fh.service({ | |
| "guid": serviceId, | |
| "path": req.path, | |
| "method": req.method, | |
| "params": req.body | |
| }, function(err, body, serviceRes) { | |
| if ( err ) { | |
| return res.status(serviceRes && serviceRes.statusCode || 500).json(err); | |
| } | |
| return res.json(body); | |
| }); | |
| }); | |
| // fhlint-end | |
| // 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'; | |
| var server = 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