Skip to content

Instantly share code, notes, and snippets.

@DominicFinn
Last active March 28, 2023 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DominicFinn/69ba8d6a4a3a21403f8426f30959092b to your computer and use it in GitHub Desktop.
Save DominicFinn/69ba8d6a4a3a21403f8426f30959092b to your computer and use it in GitHub Desktop.
Different behaviour for different firebase functions
const functions = require("firebase-functions");
const express = require("express");
let staticValue = { message: 'Hello World!' }
// you don't want this then as it will be a single instance of helloWorld_app. You don't really have control when the instance is up or down unless
// you configure that in the function with "minInstances" etc
const app = express();
app.get('/short', (req, res) => {
setTimeout(() => {
staticValue.message = 'Hello short!';
res.send(staticValue.message);
}, 2000);
});
app.get('/long', (req, res) => {
setTimeout(() => {
res.send(staticValue.message);
}, 5000);
});
exports.helloWorld_app = functions.https.onRequest(app);
// you want these and then map a nice route in hosting if you want it
// we are actually running in different vm's so this is fine
exports.helloWorld_short = functions.https.onRequest((req, res) => {
setTimeout(() => {
staticValue.message = 'Hello short!';
res.send(staticValue.message);
}, 2000);
});
// yeah we are lol
exports.helloWorld_long = functions.https.onRequest((req, res) => {
setTimeout(() => {
res.send(staticValue.message);
}, 5000);
});
// each request here will be a new instance of helloWorld_random so it will only have it's own context too
exports.helloWorld_random = functions.https.onRequest((req, res) => {
const random = Math.round(Math.random() * 10000);
functions.logger.log(`waiting milliseconds: ${random}`);
setTimeout(() => {
staticValue.message = `Hello random ${random}`;
res.send(staticValue.message);
}, random);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment