Created
March 16, 2018 22:42
-
-
Save briansalvattore/dfaa0ebcef652945ac11590671f85f28 to your computer and use it in GitHub Desktop.
Proxy to run Firebase Functions
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
'use strict'; | |
const admin = require('firebase-admin'); | |
module.exports = function (app) { | |
app.get('/api', function (req, res) { | |
res.json({ 'version': '0.1' }) | |
}); | |
app.post('/api/jedi', function (req, res) { | |
admin.database().ref('/jedi').push().set({ | |
'name': req.body.name, | |
'race': req.body.race, | |
'planet': req.body.planet, | |
'color': req.body.color | |
}) | |
res.json({ 'success': true }) | |
}) | |
app.get('/api/jedi', function (req, res) { | |
admin.database().ref('/jedi').once('value').then(function(snapshot) { | |
res.json(snapshot) | |
}) | |
}) | |
} |
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
'use strict'; | |
const admin = require('firebase-admin'); | |
const express = require('express'); | |
/** begin configuration for cloud */ | |
const functions = require('firebase-functions'); | |
const serviceAccount = require('./serviceAccountKey.json'); | |
admin.initializeApp({ | |
credential: admin.credential.cert(serviceAccount), | |
databaseURL: 'https://jedi-backend-demo.firebaseio.com' | |
}); | |
var app = express(); | |
/** end configuration for cloud */ | |
/** begin config handlebars */ | |
const hbs = require('express-hbs') | |
app.engine('hbs', hbs.express4({ | |
partialsDir: './views/partials', | |
layoutsDir: './views' | |
})) | |
app.set('views', './views') | |
app.set('view engine', 'hbs') | |
/** end config handlebars */ | |
/** add api */ | |
require('./api')(app); | |
/** deploy firebase */ | |
exports.app = functions.https.onRequest(app); |
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": "functions", | |
"description": "Jedi Backend", | |
"scripts": { | |
"serve": "firebase serve --only functions,hosting", | |
"shell": "firebase experimental:functions:shell", | |
"start": "npm run shell", | |
"deploy": "firebase deploy --only functions,hosting", | |
"logs": "firebase functions:log" | |
}, | |
"dependencies": { | |
"express": "4.15.3", | |
"express-hbs": "1.0.4", | |
"firebase": "^3.1", | |
"firebase-admin": "~5.8.1", | |
"firebase-functions": "^0.8.1" | |
}, | |
"private": true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment