Created
June 21, 2017 15:47
-
-
Save mmetting/f68662a1e72bb0578c682ee3b30ebf19 to your computer and use it in GitHub Desktop.
Add another client side button to list all DB entries (MBaaS Service)
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
var express = require('express'); | |
var bodyParser = require('body-parser'); | |
var cors = require('cors'); | |
var $fh = require('fh-mbaas-api'); | |
function helloRoute() { | |
var hello = new express.Router(); | |
hello.use(cors()); | |
hello.use(bodyParser()); | |
hello.get('/', function (req, res) { | |
console.log(new Date(), 'In hello route GET / req.query=', req.query); | |
var world = req.query && req.query.hello ? req.query.hello : 'World'; | |
var readOptions = { | |
"act": "list", | |
"type": "myFirstEntity", | |
"eq": { | |
"name": world | |
} | |
}; | |
$fh.db(readOptions, function (err, data) { | |
if (err) { | |
console.error("Error " + err); | |
res.status(500).json({ msg: err }) | |
} else { | |
console.log(JSON.stringify(data)); | |
if (data.count == 0) { | |
var timestamp = new Date().getTime(); | |
var options = { | |
"act": "create", | |
"type": "myFirstEntity", | |
"fields": { | |
"name": world, | |
"timestamp": timestamp | |
} | |
}; | |
$fh.db(options, function (err, data) { | |
if (err) { | |
console.error("Error " + err); | |
res.status(500).json({ msg: err }) | |
} else { | |
console.log(JSON.stringify(data)); | |
res.status(200).json({ msg: data }) | |
} | |
}); | |
} else { | |
console.log("Entry already exists"); | |
res.status(200).json({ msg: "Entry already exists" }); | |
} | |
} | |
}); | |
}); | |
hello.get('/list', function (req, res) { | |
console.log(new Date(), 'In hello-list route GET'); | |
var readOptions = { | |
"act": "list", | |
"type": "myFirstEntity" | |
}; | |
$fh.db(readOptions, function (err, data) { | |
if (err) { | |
console.error("Error " + err); | |
res.status(500).json({ msg: err }) | |
} else { | |
console.log(JSON.stringify(data)); | |
res.status(200).json({ msg: data }) | |
} | |
}); | |
}); | |
return hello; | |
} | |
module.exports = helloRoute; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment