Last active
September 1, 2017 17:53
-
-
Save mabez/5ab18e74e7c60f4209bc0c11a2594b5f to your computer and use it in GitHub Desktop.
Uma function do google cloud que conecta com o firebase
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 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); | |
}); |
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
{ | |
"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