Skip to content

Instantly share code, notes, and snippets.

@mmetting
Created June 21, 2017 15:47
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 mmetting/f68662a1e72bb0578c682ee3b30ebf19 to your computer and use it in GitHub Desktop.
Save mmetting/f68662a1e72bb0578c682ee3b30ebf19 to your computer and use it in GitHub Desktop.
Add another client side button to list all DB entries (MBaaS Service)
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