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

MyMemoWiki

Cloud Functions

提供: MyMemoWiki
2020年11月2日 (月) 15:15時点におけるPiroto (トーク | 投稿記録)による版 (→‎カスタムドメイン)
ナビゲーションに移動 検索に移動

| Google Cloud Platform | Firebase |

Cloud Functions

ローカル実行

  • Firebase エミュレータを呼び出す

**package.json で定義された、firebase emulators:start を実行

$ npm install -g firebase-tools
$ cd functions
$ npm run serve

Expressと統合

$ npm install --save express
$ npm install --save-dev @types/express
  • index.ts
import * as functions from 'firebase-functions';
import * as express from 'express';
const app: express.Express = express();

const router: express.Router = express.Router()
app.use(router);

router.get('/test', (req, res) => {
    res.send('TEST!');
});

export const api = functions.https.onRequest(app);

カスタムドメイン

  1. Hostingと併用することで対応
  2. Hostingに対してカスタムドメインを設定
  3. firebase.json の hosting の rewrite に functions を追加
  • Functions
import * as functions from 'firebase-functions';
import * as express from 'express';

const app: express.Express = express();

router.get('/api/test', (req, res) => {
    res.send('TEST!');
});

export const apiService = functions.https.onRequest(app);
  • firebase.json
    • パスもapi、公開(export)する関数も apiだと、パスにマッチしないため、別の名前にする
  "hosting": [
    {
         : 省略
      "rewrites": [
        {
          "source": "/api/**",
          "function": "apiService"
        },
        {
          "source": "**",
          "destination": "/index.html"
        }
      ]
    }
  ]
  • 実行

Functions custom domain.png