Firebase Cloud FunctionsのHTTPトリガーを使ってみる

Firebase Cloud FunctionsのHTTPトリガーを使ってみる

前提

Blazeプラン(従量課金)であること。※Sparkプランだとデプロイでエラーになります

Error: Your project takahashiproj must be on the Blaze (pay-as-you-go) plan to complete this command. Required API cloudbuild.googleapis.com can't be enabled until the upgrade is complete. To upgrade, visit the following URL:


Sign in - Google Accounts
Having trouble? Try firebase [command] --help

プロジェクト作成

firebase init functionsコマンドを実行します。

C:\test>firebase init functions

jsかtsを選択できますがここではjsを選択しています。

WindowsでFirebase Cloud FunctionsのHTTPトリガーを使ってみる

  • Do you want to use ESLint to catch probable bugs and enforce style? Y
  • Do you want to install dependencies with npm now? Y

上記2つとも共に「Y」にします。

WindowsでFirebase Cloud FunctionsのHTTPトリガーを使ってみる

functionsフォルダが作成されます。このフォルダがnodeプロジェクトになっていることがわかります。

index.js

functionsフォルダのindex.jsに関数を追加していってHTTPトリガー(HTTP関数)を実装していきます。

const functions = require("firebase-functions");
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {
  functions.logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Firebase!");
});

デフォルトでは下から4行がコメントされているので、そのコメントを外しました。

このindex.jsにHTTPトリガー(HTTP関数)を実装したら最後にデプロイする、という流れです。

C:\test>firebase deploy

このコマンドでデプロイします。

WindowsでFirebase Cloud FunctionsのHTTPトリガーを使ってみる

「Deploy complete!」と表示されたらOKです。その下に「Project Console:~~」と表示されているURLを開きます。

WindowsでFirebase Cloud FunctionsのHTTPトリガーを使ってみる

「helloWorld」という関数が追加されています。

HTTP関数実行

curlコマンドで実行してみます。

C:\test>curl -X GET https://us-central1-takahashiproj.cloudfunctions.net/helloWorld
Hello from Firebase!

正常に標準出力されました。

HTTPリクエスト経由で関数を呼び出しできるのがHTTPトリガーとなります。

関数はindex.jsに別名で追加していきます。

const functions = require("firebase-functions");

exports.helloWorld = functions.https.onRequest((request, response) => {
  functions.logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Firebase!");
});

exports.helloWorldquery = functions.https.onRequest((request, response) => {
  if (request.query.key !== undefined) {
    response.send("key = " + request.query.key);
  } else {
    response.send("invalid query parameter name.");
  }
});

index.jsを編集したらデプロイします。

C:\test>firebase deploy

WindowsでFirebase Cloud FunctionsのHTTPトリガーを使ってみる

正常デプロイ出来たら、コンソールに新たな関数(helloworldquery)が追加されていることが確認できます。

request

curlでコマンド実行した際の値をrequestから取得します。(Content-Type:application/json)

curl -X POST -H "Content-Type:application/json" -H "X-MyHeader: 123" https://エンドポイントURL/?key=value -d '{"name":"Bob"}'
プロパティ/メソッド
request.query.key value クエリパラメータ
request.method POST HTTPメソッド
request.body.name Bob リクエストボディ
request.get(‘X-MyHeader’) 123 ヘッダ情報
request.rawBody リクエストのRAW(未解決の)バイト数

東京リージョンに変更

リージョン省略時は、アイオワリージョン(us-central1)になります。

東京リージョンに変更するには.region('asia-northeast1')を追加します。

exports.helloWorld = functions.https.onRequest~
↓
exports.helloWorld = functions.region('asia-northeast1').https.onRequest~

これで東京リージョン(asia-northeast1 )に変更されます。

Firebase Cloud FunctionsのHTTPトリガーを使ってみる

ローカルテスト

デプロイする前に必ずローカルでテストします。

emulatorを起動します。functionsはデフォルトポートは5001です。

C:\>firebase emulators:start --only functions

URL形式は以下の通りです。

http://localhost:5001/<プロジェクトID>/<リージョン>/<関数名>

このURLをcurlコマンドで叩けばテストすることが出来ます。

ドキュメント

https://firebase.google.com/docs/functions/http-events?hl=ja

コメント

タイトルとURLをコピーしました