Skip to content

Instantly share code, notes, and snippets.

@aerobless
Last active June 28, 2018 12:27
Show Gist options
  • Save aerobless/3727bee86a141eebe25d492028abdee6 to your computer and use it in GitHub Desktop.
Save aerobless/3727bee86a141eebe25d492028abdee6 to your computer and use it in GitHub Desktop.
Example of how to do routing in a Google Cloud Function
const router = require('gcf-api-router')();
router.route('/result')
.get(getAllResults) //GET api/result
.post(storeResult); //POST api/result
router.route('/result/:id')
.get(getSpecificResult); //GET api/result/[id]
function getAllResults(req, res) {
//get all results
res.send('returning results..');
}
function storeResult(req, res) {
const homeTeamScore = JSON.parse(req.body).homeTeamScore;
//store result
res.send('result posted: ' + homeTeamScore);
}
function getSpecificResult(req, res) {
const idParam = req.params.id
//get result
res.send('returning result with id: ' + idParam)
}
//This function (entrypoint) is configured in the Cloud Functions UI
exports.entrypoint = function (req, res) {
// Some request processing/verification/logging can be done here
router.onRequest(req, res);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment