How to use firebase cloud functions

Issue #667

Use node 10

Edit package.json

1
2
3
"engines": {
"node": "10"
},

Secret key

Go to settings/serviceaccounts/adminsdk, download secret key in form of json and place it in lib/config.json

1
2
3
4
5
6
const serviceAccount = require('./config.json')

admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://my-app-4b968.firebaseio.com"
})

Local serve

This builds and spins up emulator to test

1
npm run serve

CORS

1
response.set('Access-Control-Allow-Origin', '*');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var cors = require('cors');

// my function
var helloFn = function helloFn(req, res) {
res.status(200)
.send('Hello, Functions\n');
};

// CORS and Cloud Functions export logic
exports.hello = function hello(req, res) {
var corsFn = cors();
corsFn(req, res, function() {
helloFn(req, res);
});
}

Updated at 2020-06-14 21:39:27

Comments