@JunQiu
2018-09-20T08:37:22.000000Z
字数 2176
阅读 2050
tools summary_2018/09
### Example 接受多种方法,只是需要对应方法处理对应的数据,比如body,queryexports.helloWorld = (req, res) => {if (req.body.message === undefined) {res.status(400).send('No message defined!')} else {console.log(req.body.message)res.status(200).end()}}Tips:如果要实现多种请求方法,可以通过req中的method来实现
## pub/sub 实例exports.helloPubSub = (event) => {const pubsubMessage = event.dataconst name = pubsubMessage.data ? Buffer.from(pubsubMessage.data, 'base64').toString() : 'World'console.log(`Hello, ${name}!`)}Tips:文档中有一个回调函数callback用于发出信号来指示您的函数已完成运行,但实际使用中没有必要,直接打印日志即可。在google cloud function中:console.log() 命令具有 INFO 日志级别。console.error() 命令具有 ERROR 日志级别。
### 本地# 使用gcloud 命令行工具gcloud beta functions deploy <NAME> <TRIGGER>- NAME: Cloud Functions 函数的名称- TRIGGER:触发方式// 部署(trigger为http的刚才编写的函数 helloWorld)gcloud beta functions deploy helloWorld --trigger-http生成url:https://us-east1-ai-dev-216405.cloudfunctions.net/helloWorld// 调用使用http请求即可实现请求# 仓库格式:gcloud beta functions deploy <NAME> \--source https://source.developers.google.com/projects/<PROJECT_ID>/repos/<REPOSITORY_ID>/moveable-aliases/master/paths/<SOURCE> \<TRIGGER>(--source:源代码仓库的位置)// Example:以仓库function-pause-ad为例gcloud beta functions deploy pauseAd --source https://source.developers.google.com/projects/ai-dev-216405/repos/function-pause-ad/moveable-aliases/master/paths/ --trigger-http// 当然失败了,因为该函数是后台函数// 调用方式相同
