Last active
March 28, 2023 11:52
-
-
Save DominicFinn/69ba8d6a4a3a21403f8426f30959092b to your computer and use it in GitHub Desktop.
Different behaviour for different firebase functions
This file contains 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
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