Last active
June 28, 2018 12:27
-
-
Save aerobless/3727bee86a141eebe25d492028abdee6 to your computer and use it in GitHub Desktop.
Example of how to do routing in a Google Cloud Function
This file contains hidden or 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 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