| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

差分

ナビゲーションに移動 検索に移動
2,005 バイト追加 、 2020年11月9日 (月) 14:04
===ローカル実行===
*Firebase エミュレータを呼び出す
**package.json で定義された、firebase emulators:start を実行
*https://firebase.google.com/docs/functions/local-emulator
<pre>
$ npm install -g firebase-tools
$ cd functions
$ npm run serve
</pre>
===[[Express]]と統合===
*https://taroosg.io/cloud-functions-express
<pre>
$ npm install --save express
$ npm install --save-dev @types/express
</pre>
*https://firebaseindex.google.com/docs/functions/local-emulatorts
<pre>
$ npm install -g import * as functions from 'firebase-toolsfunctions';import * as express from 'express';const app: express.Express = express(); const router: express.Router = express.Router()app.use(router); router.post('/test', (req, res) => { res.send('TEST!');}); export const api = functions.https.onRequest(app);
</pre>
===https.onCall のプロトコル仕様===*https://firebase.google.com/docs/functions/callable-reference*呼び出し可能なトリガー エンドポイントへの HTTP リクエストは、次のヘッダーが含まれる POST にする必要があります。**必須: Content-Type: application/json**省略可: Authorization: Bearer <token>:リクエストを行うログイン済みユーザーの Firebase Authentication ユーザー ID トークンです。このトークンはバックエンドで自動的に検証され、ハンドラの context で使用可能になります。トークンが有効でない場合、リクエストは拒否されます。**省略可: Firebase-Instance-ID-Token: <iid>:Firebase クライアント SDK の FCM 登録トークンです。これには文字列を設定する必要があります。またハンドラの context で使用できます。プッシュ通知のターゲティングに使用されます。 ===カスタムドメイン===*[https://consoleblog.cloudkatsubemakito.net/firebase/cloudfunctions-restfulapi-domain-rewrite*https://firebase.google.com/iamdocs/hosting/full-adminconfig?hl=ja*https:/serviceaccounts サービス アカウント] ペインを開きます。/firebase.google.com/docs/hosting/full-config?hl=ja#rewrites*App Engine default service account が選択されていることを確認し、右側のオプション メニューで [キーを作成https://firebase.google.com/docs/hosting/full-config?hl=ja#glob_pattern_matching globパターンマッチング] を選択します。*プロンプトが表示されたら、キーのタイプとして JSON を選択し、[作成] をクリックします。#Hostingと併用することで対応#Hostingに対してカスタムドメインを設定#firebase.json の hosting の rewrite に functions を追加 *ダウンロードしたキーを参照するように Google のデフォルトの認証情報を設定します。Functions
<pre>
$ export GOOGLE_APPLICATION_CREDENTIALSimport * as functions from 'firebase-functions';import * as express from 'express'; const app: express.Express ="pathexpress(); router.get('/toapi/keytest', (req, res) => { res.json"send('TEST!');$ firebase emulators:start}); export const apiService = functions.https.onRequest(app);
</pre>
*上記は、firebase init で有効化した機能に依存するが、特定の機能のエミュレータを起動する場合firebase.json**パスもapi、公開(export)する関数も apiだと、パスにマッチしないため、別の名前にする
<pre>
$ firebase emulators "hosting":start --only functions[ { : 省略 "rewrites": [ { "source": "/api/**", "function": "apiService" }, { "source": "**", "destination": "/index.html" } ] } ]
</pre>
*実行
[[File:functions_custom_domain.png|400px]]

案内メニュー