20
loading...
This website collects cookies to deliver better user experience
$ firebase functions:config:set [scope].[value]="something"
hello
to a variable called myvariable
in the scope of test
, run the following code:$ firebase functions:config:set test.myvariable="hello"
$ firebase functions:config:get
{
"test": {
"myvariable": "hello"
}
}
$ firebase functions:config:set test.myvariable="hello, world!"
bash: !": event not found
$ firebase functions:config:set test.myvariable='hello, world!'
const functions = require('firebase-functions');
// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions
exports.helloWorld = functions.https.onRequest((request, response) => {
functions.logger.info("Hello logs!", {structuredData: true});
response.send("Hello from Firebase!");
});
myvariable
environment variable we've setup. To retrieve all the environment variables in our project, we can write functions.config()
within our code, and then we can access the environment variable by specifying the path to the variable. Since myvariable
is part of the test
scope, our code should look like thisconst functions = require('firebase-functions');
// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions
exports.helloWorld = functions.https.onRequest((request, response) => {
functions.logger.info("Hello logs!", {structuredData: true});
response.send(functions.config().test.myvariable);
});
$ firebase emulators:start
$ firebase functions:config:get > .runtimeconfig.json
.runtimeconfig.json
. This is a file that the emulator is able to read from. Let's restart the emulator and see if we can read our environment variable now:firebase functions:config:get > .runtimeconfig.json
script. In addition, once the emulator is started it caches the values within the runtimeconfig file, so you'll need to restart the emulator to have the new environment variables picked up.functions.https.onRequest()
method accept the Express router configuration.const functions = require('firebase-functions');
const express = require('express');
const cors = require('cors');
const app = express();
// Automatically allow cross-origin requests
app.use(cors({origin: true}));
// Defines my GET method at the helloWorld endpoint
app.get('/', (request, response) => {
response.end('Here is my GET request!');
});
// Defines my POST method at the helloWorld endpoint and prints out an environment variable
app.post('/', (request, response) => {
response.end('Here is my POST request! ' + functions.config().test.myvariable);
});
// Defines my PUT method at the helloWorld endpoint
app.put('/', (request, response) => {
response.end('Here is my PUT request!');
});
// Expose Express API as a single Cloud Function
exports.helloWorld = functions.https.onRequest(app);
$ curl -X POST http://localhost:5001/irbytestproject/us-central1/helloWorld
Here is my POST request! hello, world!
$ curl -X PUT http://localhost:5001/irbytestproject/us-central1/helloWorld
Here is my PUT request!
$ firebase deploy
$ curl -X POST https://us-central1-irbytestproject.cloudfunctions.net/helloWorld
Here is my POST request! hello, world!
20