JavaScript

firebase RESTful API 작성

jmeen 2021. 9. 10. 17:46
728x90

firebase 웹 서버를 이용하여 채팅 어플리케이션을 만들어 볼 것이다.

먼저 웹 서버를 준비하는 과정이다.

 

firebase 

Firebase (google.com)

 

Firebase

Firebase는 고품질 앱을 빠르게 개발하고 비즈니스를 성장시키는 데 도움이 되는 Google의 모바일 플랫폼입니다.

firebase.google.com

 

 

1. 프로젝트 로컬 폴더 생성 후 code로 열기

 

2. firebase 프로젝트 만들기

 

2-1. 프로젝트 이름 입력 후 계속

 

2-2. 이번 프로젝트에는 구글 애널리틱스가 필요없다. 사용안함 후 프로젝트 만들기 실행

 

3. VS CODE - firebase CLI 설치

npm install -g firebase-tools

 

4. filebase로그인

firebase login

4. filebase project 목록 확인

firebase projects:list

5. Hosting 설정

firebase init hosting

6. firebase 펑션 설정

firebase init functions

7. firebase database 설정

firebase init database

 

8.프로젝트 설정

 

 

9. 테스트

 firebase serve --only functions

10. database rules.json

{
  /* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
  "rules": {
    ".read": true,
    ".write": true
  }
}

11. API test - git bash로 수행

curl -H 'Content-Type:application/json' -d '{"cname": "general"}' http://localhost:5000/chat-server-1b34b/us-central1/v1/channels

 

curl  http://localhost:5000/chat-server-1b34b/us-central1/v1/channels

curl -H 'Content-Type:applicat/json' -d '{}' http://localhost:5000/chat-server-1b34b/us-central1/v1/reset

 

 

$. API 테스트 더 쉽게하는 POSTMAN / 11번의 내용을 더 쉽게 할 수 있다. 서버가 돌고 있어야 한다.

Download Postman | Try Postman for Free

 

Download Postman | Try Postman for Free

Try Postman for free! Join 17 million developers who rely on Postman, the collaboration platform for API development. Create better APIs—faster.

www.postman.com

 

 

curl -H 'Content-Type:application/json' -d '{"cname": "general"}' http://localhost:5000/chat-server-1b34b/us-central1/v1/channels 를 세팅해보자

 

1. header

2. 

POST인지, GET인지만 구분해서 넣어주면 쉽게 세팅할 수 있다. 세팅한 값은 저장이 가능하니 틈틈히 열어 확인하기 간편하다.