LoginSignup
10
10

More than 5 years have passed since last update.

Cloud Functions for Firebase 覚書き

Last updated at Posted at 2018-02-10

Cloud Functions for Firebase に関する個人的な/初歩的な備忘録です.


ローカルでFunctionsのAPIをテストする

$ firebase serve --only functionsでテスト用ローカルサーバが起動する.
$ firebase serve --only functions,hostingとすればHosting用のサーバも起動する.

  • ローカルでのFunctionsのAPI呼び出しは手元の環境のNode.js上で実行されるため、実際にFirebase Functionsの本番環境で使用されているNode.jsとバージョンが異なる場合は微妙な相違に注意する必要がある. *
  • $ firebase serveだとFunctionsのAPI呼び出しは本番環境のものが使用されることに注意

参考:

Hostingのドメイン上でFunctionsのAPIを呼ぶ

firebase.jsonrewritesを書き換えることでFirebase Hosting上のページから同一ドメインのURLでFunctionsのAPIを呼べる.
例えば以下のようにするとhttps://<Hostingのドメイン>/api/helloWorldへのアクセスでFunctions側の関数helloWorldが呼ばれる.

firebase.json
{
  "hosting": {
    ...
    "rewrites": [
      {"source": "/api/helloWorld", "function": "helloWorld"},
      ...
    ]
  },
  ...
}

$ firebase serve --only functions,hostingでローカルサーバを起動した際にも、ローカルのHosting用アドレスhttp://localhost:5000/api/helloWorldでFunctionsのAPIをテストできる.
HostingとFunctionsとで同一ドメインなためCORSの問題が発生しなくて済むことや、ローカルのテスト環境とデプロイした本番環境とで/api/helloWorldで共通してAPI用URLを呼べるため便利.

参考:

async/awaitが使えない

今現在 (2018/02/10) Functionsで使われているNode.jsのバージョンが古いため、ピュアなJSのコードではasyncawaitが使えない. またObject.values()など他にも地味に使えないものがある.

これらの機能を使うには素直にトランスパイラを通す必要がでてくる.

参考:

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10