본문 바로가기

other(그 밖의)

Firebase cloud function 사용방법

반응형

필수 설정 

1. Node.js

2. npm(Node Package Manager)

3. Firebase project 

 이 글에서는 위에 필수 설정이 모두 준비 됐다는 가정하에 작성하겠습니다!

 


설치

우선 npm으로 firebase tool을 다운받습니다

npm install -g firebase-tools

만약 권한에 대한 에러가 난다면 

sudo npm install -g firebase-tools

 

 

이렇게 입력해주면 잘 작동할것입니다 

사용 언어는 js를 사용할 것이기 때문에 js 선택해줍니다 나머지는 Y를 눌러도 무방합니다 

firebase login

로그인을 해줍니다 

Allow Firebase to collect CLI usage and error reporting information? 

오류 정보를 수집할 수 있게 허용할 것냐고 묻는데 저는 Yes를 했습니다 입맛대로 선택하시면 될거같습니다 

 이제 터미널에서 firebase 프로젝트 디렉토리로이동합니다 

firebase init functions

 

 

설치를 해주신다면 

myproject
 +- .firebaserc    # Hidden file that helps you quickly switch between
 |                 # projects with `firebase use`
 |
 +- firebase.json  # Describes properties for your project
 |
 +- functions/     # Directory containing all your functions code
      |
      +- .eslintrc.json  # Optional file containing rules for JavaScript linting.
      |
      +- package.json  # npm package file describing your Cloud Functions code
      |
      +- index.js      # main source file for your Cloud Functions code
      |
      +- node_modules/ # directory where your dependencies (declared in
                       # package.json) are installed

이런 구조가 될것입니다 


설정 

이제 설치를 하면서 기본적인 설정을 하셨으니 node에서 설정을 해봅시다 !

우선 firebase cloud function을 사용 하기위한 모듈을 가지고 와야 합니다 

 

index.js

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

const admin = require('firebase-admin); //firestore에 접근할수있는 adminSDK
admin.initializeApp();//초기화

const functions = require("firebase-functions");
const admin = require("firebase-admin"); 
const { response } = require("express");
admin.initializeApp();

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
//  exports.helloWorld = functions.https.onRequest((request, response) => {
//    functions.logger.info("Hello logs!", {structuredData: true});
//    response.send("Hello from Firebase!");
//  });

 

이제 하단에 주석처리되어있는 코드를 주석을 풀어줍니다 

이제 한번 배포를 해봅시다!

vscode 하단 터미널에

firebase deploy --only funtion

입력해주세요 이렇게 하면 export한 함수들이 모두 배포가 됩니다 

만약 특정 클라우드 함수만 배포를 하고 싶으시다면

firebase deploy --only functions:helloWorld

이렇게 해주시면 됩니다 

이제 프로젝트 좌측에 있는 functions 탭을 누르시고 확인을 해보신다면

이렇게 함수가 잘 들어와있네요 

url을 타고 들어가보시면

이렇게 잘 나오는걸 알 수 있네요

만약 

Error: functions predeploy error: Command terminated with non-zero exit code1

https://chicken-coach.tistory.com/14

 

Firebase Node deploy 에러 해결방법 총정리

이제 막 node 에서 firebase의 cloud funtion을 사용하려고 설치를 한 후 테스트를 위해 실행을 했지만 Error: functions predeploy error: Command terminated with non-zero exit code1 라는 오류가 발..

chicken-coach.tistory.com

 

제가 겪어서 해결한걸 정리해 놓았으니 참고하시면 좋을거같습니다 그리고

함수명 하단에 리전이 적혀있는데 지역을 변경하고싶으시다면 

exports.helloWorld = functions
	.region("asia-northeast3") //서울
    .https.onRequest((request, response) => {
    functions.logger.info("Hello logs!", {structuredData: true});
    response.send("Hello from Firebase!");
 });

이렇게 사용하시면 된답니다 

반응형