LoginSignup
5
3

More than 5 years have passed since last update.

Google Cloud Functions for Firebaseを試してみた(環境構築編)

Posted at

Google Cloud FunctionsというものがBetaで公開されました。

詳しくはこの動画で紹介されています。

IMAGE ALT TEXT HERE

Google Cloud Functions for Firebaseとは?

簡単にいうと、Firebaseでできる機能(ユーザ認証、DB、ストレージの追加/変更)のイベントを基にサーバ側の処理を提供するサービスのようです。

2017/03/13現在、JavaScript(Node.js)しかつかえないようです。
基本的にCLIで管理するようで、CLIリファレンスはこちらに掲載されています。

初期設定

前提として、Node.jsとnpmが使える必要があります。
Node.jsの環境構築はこちらを参考にしてください。

Firebase Tools SDKのインストール

npmでインストールします。

Terminal
$ npm install -g firebase-tools

色々メッセージは出ますが、エラーにならなければOKです。

ログイン

SDKのインストールができたら、Googleアカウントでログインします。

Terminal
$ firebase login

作業ディレクトリの準備

ログインができたら、作業ディレクトリを準備します。
作業したいディレクトリ(私の場合は/Users/furusin/workspace/nodejs/Google_Cloud_Function/GCF_testとしました)を作成し、プロジェクト ディレクトリを初期化します。

Terminal
$ cd 作業ディレクトリの場所
$ firebase init

firebase initコマンドを実行すると、必要なファイルの作成が始まります。
スクリーンショット 2017-03-13 10.31.15.png

処理の作成

~/functions/index.jsに処理を書いていきます。

デフォルトではこのようになっているので、自由に記載します。

index.js
var functions = require('firebase-functions');

// // Start writing Firebase Functions
// // https://firebase.google.com/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// })

デプロイ

Terminal
$ firebase deploy

これでデプロイできます。

 まとめ

簡単にリファレンスにあることをまとめただけですが、非常に簡単に環境が構築できました。

おそらくこれを使えば、サーバレスでRest APIが構築できそうですね。ワクワクします。

次は実際にNode.jsでAPIを立ててみようと思います。

5
3
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
5
3