- Google Cloud Platform Cookbook
- Legorie Rajan PS
- 441字
- 2021-08-27 19:13:25
How to do it...
We'll use the simple calculator JavaScript code available on the book's GitHub repository and deploy it to Cloud Functions:
- Navigate to the /Chapter01/calculator folder. The application code is present in index.js and the dependencies in the package.json file. As there are no dependencies for this function, the package.json file is a basic skeleton needed for the deployment.
- The main function receives the input via the request object, validates the inputs and performs the calculation. The calculated result is then sent back to the requester via the response object and an appropriate HTTP status code. In the following code, the switch statement does the core processing of the calculator, do spend some time on it to understand the gist of this function:
/** * Responds to any HTTP request that provides the below JSON
message in the body. * # Example input JSON : {"number1": 1, "operand": "mul",
"number2": 2 } * @param {!Object} req Cloud Function request context. * @param {!Object} res Cloud Function response context. */ exports.calculator = function calculator(req, res) { if (req.body.operand === undefined) { res.status(400).send('No operand defined!'); }
else { // Everything is okay console.log("Received number1",req.body.number1); console.log("Received operand",req.body.operand); console.log("Received number2",req.body.number2); var error, result; if (isNaN(req.body.number1) || isNaN(req.body.number2)) { console.error("Invalid Numbers"); // different logging error = "Invalid Numbers!"; res.status(400).send(error); } switch(req.body.operand) { case "+": case "add": result = req.body.number1 + req.body.number2; break; case "-": case "sub": result = req.body.number1 - req.body.number2; break; case "*": case "mul": result = req.body.number1 * req.body.number2; break; case "/": case "div": if(req.body.number2 === 0){ console.error("The divisor cannot be 0"); error = "The divisor cannot be 0"; res.status(400).send(error); } else{ result = req.body.number1/req.body.number2; } break; default: res.status(400).send("Invalid operand"); break; } console.log("The Result is: " + result); res.status(200).send('The result is: ' + result); } };
- We'll deploy the calculator function using the following command:
$ gcloud beta functions deploy calculator --trigger-http
The entry point for the function will be automatically taken as the calculator function. If you choose to use another name, index.js, the deploy command should be updated appropriately:

- You can test the function via the Console, your favorite API testing apps such as Postman or via the following curl command. The endpoint for the function can be found under the Triggering event tab in the Console or it will be provided after the deploy command:
Input JSON : {"number1": 1, "operand": "mul", "number2": 2 } $ curl -X POST
https://us-central1-<ProjectID>.cloudfunctions.net/calculator
-d '{"number1": 1, "operand": "mul", "number2": 2 }' -H "Content-Type: application/json" The result is: 2
- You can also click on the VIEW LOGS button in the Cloud Functions interface to view the logs of the function execution:

推薦閱讀
- 高效能辦公必修課:Word圖文處理
- Hands-On Internet of Things with MQTT
- Learning Microsoft Azure Storage
- Dreamweaver CS3網頁制作融會貫通
- Visual C# 2008開發技術詳解
- 工業機器人工程應用虛擬仿真教程:MotoSim EG-VRC
- Windows內核原理與實現
- 21天學通Java Web開發
- 3D Printing for Architects with MakerBot
- 大數據驅動的設備健康預測及維護決策優化
- 軟件構件技術
- Deep Learning Essentials
- 菜鳥起飛電腦組裝·維護與故障排查
- Hands-On Agile Software Development with JIRA
- 工廠電氣控制設備