ลองเล่น Firebase Cloud Function


Create first Firebase cloud function

Install Firebase tools:

$ npm install -g firebase-tools

Firebase Login

$ firebase login

Init App

$ firebase init
  • select Functions
    1.png

  • use exist project or new
    2.png

Edit 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!");
// });

เป็น

const admin = require('firebase-admin');
const functions = require('firebase-functions);

admin.initializeApp(functions.config().firebase);
exports.helloWorld = functions.https.onRequest((request, response) => {
  response.send("Hello from Firebase!");
});

3.png

Deploy App

$ firebase deploy --only functions  

หรือ

$ npm run deploy

4.png

Run Service

https://us-central1-<project_id>.cloudfunctions.net/helloWorld

5.png

August 1, 2020