酢ろぐ!

カレーが嫌いなスマートフォンアプリプログラマのブログ。

Cloud Functions for Firebaseを使ってhello worldを表示してみた

node.jsに対する苦手意識があるのでCloud Functions for Firebaseを使いながらnode.jsに慣れていきたいと思います。

f:id:ch3cooh393:20170825233817p:plain

$ npm install -g firebase-tools

インストールができたみたいです。次に作業ディレクトリを作って下図の通りコマンドを実行していきます。

f:id:ch3cooh393:20170825234413p:plain

$ cd /Users/ch3cooh/Desktop/ikatomo2 
$ firebase init

Error: Command requires authentication, please run firebase login

あれ、エラーがでました。firebase loginを実行するとブラウザが立ち上がってGoogleアカウントにログインを求められます。

$ firebase login
? Allow Firebase to collect anonymous CLI usage information? Yes

Visit this URL on any device to log in:
https://accounts.google.com/o/oauth2/auth?zzzzz

Waiting for authentication...

✔  Success! Logged in as example@gmail.com

ログインできたので気を取り直してfirebase initを実行します。

$ firebase init

     🔥🔥🔥🔥🔥🔥🔥🔥 🔥🔥🔥🔥 🔥🔥🔥🔥🔥🔥🔥🔥  🔥🔥🔥🔥🔥🔥🔥🔥 🔥🔥🔥🔥🔥🔥🔥🔥     🔥🔥🔥     🔥🔥🔥🔥🔥🔥  🔥🔥🔥🔥🔥🔥🔥🔥
     🔥🔥        🔥🔥  🔥🔥     🔥🔥 🔥🔥       🔥🔥     🔥🔥  🔥🔥   🔥🔥  🔥🔥       🔥🔥
     🔥🔥🔥🔥🔥🔥    🔥🔥  🔥🔥🔥🔥🔥🔥🔥🔥  🔥🔥🔥🔥🔥🔥   🔥🔥🔥🔥🔥🔥🔥🔥  🔥🔥🔥🔥🔥🔥🔥🔥🔥  🔥🔥🔥🔥🔥🔥  🔥🔥🔥🔥🔥🔥
     🔥🔥        🔥🔥  🔥🔥    🔥🔥  🔥🔥       🔥🔥     🔥🔥 🔥🔥     🔥🔥       🔥🔥 🔥🔥
     🔥🔥       🔥🔥🔥🔥 🔥🔥     🔥🔥 🔥🔥🔥🔥🔥🔥🔥🔥 🔥🔥🔥🔥🔥🔥🔥🔥  🔥🔥     🔥🔥  🔥🔥🔥🔥🔥🔥  🔥🔥🔥🔥🔥🔥🔥🔥

You're about to initialize a Firebase project in this directory:

  /Users/ch3cooh/Desktop/ikatomo2

? Which Firebase CLI features do you want to setup for this folder? Press Space 
to select features, then Enter to confirm your choices. Database: Deploy Firebas
e Realtime Database Rules, Functions: Configure and deploy Cloud Functions

=== Project Setup

First, let's associate this project directory with a Firebase project.
You can create multiple project aliases by running firebase use --add, 
but for now we'll just set up a default project.

? Select a default Firebase project for this directory: product_name (product_name)

=== Database Setup

Firebase Realtime Database Rules allow you to define how your data should be
structured and when your data can be read from and written to.

? What file should be used for Database Rules? database.rules.json
✔  Database Rules for product_name have been downloaded to database.rules.json.
Future modifications to database.rules.json will update Database Rules when you run
firebase deploy.

=== Functions Setup

A functions directory will be created in your project with a Node.js
package pre-configured. Functions can be deployed with firebase deploy.

✔  Wrote functions/package.json
✔  Wrote functions/index.js
? Do you want to install dependencies with npm now? Yes

生成されました。

f:id:ch3cooh393:20170825235205p:plain

functions/index.jsを開きます。

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) => {
  response.send("Hello from Firebase!");
});

コメントアウト部分を削除します。Hello from Firebase!と出力するだけのjsをデプロイします。firebase deployを実行します。

$ firebase deploy

=== Deploying to 'product_name'...

i  deploying database, functions
✔  database: rules ready to deploy.
i  functions: ensuring necessary APIs are enabled...
i  runtimeconfig: ensuring necessary APIs are enabled...
⚠  functions: missing necessary APIs. Enabling now...
✔  runtimeconfig: all necessary APIs are enabled
✔  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (857 B) for uploading
✔  functions: functions folder uploaded successfully
i  starting release process (may take several minutes)...
i  functions: creating function helloWorld...
✔  functions[helloWorld]: Successful create operation. 
✔  functions: all functions deployed successfully!

✔  Deploy complete!

Project Console: https://console.firebase.google.com/project/product_name/overview
Function URL (helloWorld): https://us-central1-product_name.cloudfunctions.net/helloWorld

Firebaseコンソールから確認してもよいのですがさきほどのfirebase deployの最後に表示されているURLの https://us-central1-product_name.cloudfunctions.net/helloWorld にブラウザでアクセスすると下記のようなレスポンスが返ってきました。

f:id:ch3cooh393:20170826002219p:plain

次のステップはFirebase Databaseのデータを読み出して表示させたいですね。