-
firebase 클라우드 펑션 배포, 호출back_end 2022. 2. 2. 06:12
functions폴더 의 index.js
const functions = require("firebase-functions"); exports.testOnRequest = functions.https.onRequest((req, res) => { console.log("https.onRequest called >> testOnRequest >>", req.method); if(req.method === "GET") { console.log("[GET]testOnRequest >> API called Succeed"); return res.status(200).send({message: "[GET] API called Succeed! "}); } else if(req.method === "POST") { console.log("[POST] testOnRequest >>> API called Succeed"); return res.status(200).send({message: "[POST] API called! "}); } return res.status(400).send(">>> API called error!"); });
배포 firebase deploy --only functions
gcp 클라우드 펑션과 파이어베이스 클라우드 펑션에 동시에 등록이됨
플러터 클라이언트에서 호출
HttpsCallable callable = FirebaseFunctions.instance.httpsCallable( 'testOnRequest' //함수명 ); try { HttpsCallableResult<dynamic> response = await callable.call() }); } catch (e) { print('ERROR >>>: $e'); }
클라이언트에서 로컬에서 호출할시 앱엔진의 node js 서버에 get이나 post요청을 할때는
localhost나 127.0.0.1이 아니고 ipv4 로컬아이피로 호출하면 작동함.
파이어베이스 펑션을 로컬 개발시 디버깅할때는
FirebaseFirestore.instance.useFirestoreEmulator('로컬아이피', 클라우드 펑션 포트);
로해야 클라우드 함수가 호출된다는 인터넷 답변이 많은데..내경우는 적용이 안됨..
그냥 아예 사용안하고 HttpsCallable callable 에서 함수명만 할당하고 바로 call하니까 호출되서 해결함.
클라우드 함수 로그를 보려면 파이어베이스 에뮬레이터를 사용해야한다.
터미널에서 firebase emulators:start 명령어를 사용하면 로컬호스트:4000으로 구동되고
해당 사이트에서 펑션 로그를 확인할 수 있음.
gcp 클라우드 펑션과 파이어베이스 클라우드 펑션의 로그에서도 로그가 남는다.
'back_end' 카테고리의 다른 글
node.js - io.socket 소켓 통신 연동 (0) 2022.11.23 [스크랩]stateless, 클라이언트 http 요청 관련 싱글톤 (0) 2022.10.28 [메모/스크랩] FCM 노티 관련 메모.. (0) 2022.03.14 [스크랩/메모] GCP 방화벽 설정 관련 복붙 (0) 2022.02.03 [스크랩/메모] node js .. Cannot set headers after they are sent to the client (0) 2022.01.22