Skip to content

Instantly share code, notes, and snippets.

@mabez

mabez/index.js

Last active Sep 1, 2017
Embed
What would you like to do?
Uma function do google cloud que conecta com o firebase
var functions = require('firebase-functions');
var admin = require('firebase-admin');
var code = 404;
var result = "{\"message\": \"not found\"}";
var debug = '';
admin.initializeApp(
{...},
databaseURL: "https://amigosecreto-7d398.firebaseio.com/"
});
exports.amigos = functions.https.onRequest((req, res) => {
var db = admin.database();
var ref = db.ref("amigos");
res.set('content-type','application/json');
if (req.method == 'GET') {
if (req.query.id != {}) {
ref.once("value", function(snapshot) {
var lista = snapshot.val();
result = "{\"message\": \"" + req.query.id + " not found\"}";
if (typeof lista[req.query.id] !== 'undefined' && lista[req.query.id]) {
code = 200;
result = JSON.stringify(lista[req.query.id]);
}
});
res.status(code).end(result);
}
ref.once("value", function(snapshot) {
code = 200;
result = JSON.stringify(snapshot);
});
res.status(code).end(result);
}
var message = "{\"message\": \"" + req.method + " not implemented\"}";
res.status(405).end(message);
});
{
"name": "amigos",
"version": "0.0.1",
"dependencies": {
"firebase-functions": "0.6.3",
"firebase-admin": "5.2.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment