본문 바로가기
ETC

Firebase API 만들기

by Wilkyway 2021. 7. 27.
반응형

1. 프로젝트 생성

- 로그인 / 프로젝트 생성

- firebase.google.com

- Function 과 Hosting 기능을 사용하려면 Blaze 로 업그레이드가 필요합니다.

 

2. 로컬에 firebase 설치 및 시작

npm install -g firebase-tools

1) firebase 로그인

firebase login

2) firebase init

firebase init

생성 과정에서 lint는 y로 설치해줍니다.

public directory 의 이름을 지정해야 하는데 기본 설정인 public으로 진행하겠습니다.
(public directory 는 html, css, js 파일들이 위치하게 될 디렉토리입니다.)

rewrite all urls to /index.html 은 n 으로 입력 후 엔터를 입력합니다.

 

3. firebase directory 

1) function 폴더

function 폴더 안의 index.js 파일을 작성해서 routing을 설정할 수 있고, API서버 작동도 설정합니다. Firebase에서 제공되는 url로 접근할 수 있게됩니다. 

2) public 폴더

firebase 에서 제공되는 url 로 접근 시 보여줄 html, css, js 파일을 위치시킵니다.

3) firebase deploy

firebase deploy

 

4. API 서버 예제

1) index.js 파일 수정

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

const express = require("express");

const app1 = express();

app1.get("/hello", (request, response) => {
  response.send("Hello from Express on Firebase!");
});

const api1 = functions.https.onRequest(app1);

module.exports = {
  api1,
};

2) firebase.json 파일 수정

{
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint"
    ]
  },
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source":"/**",
        "function":"api1"
      }
    ]
  }
}

3) firebase deploy

firebase deploy

 

5. API 요청 해보기

https://us-central1-wjdb-647dc.cloudfunctions.net/api1/hello

<결과>

반응형

'ETC' 카테고리의 다른 글

Firebase 데이터 저장하기  (0) 2021.07.30
Firebase 구글 인증하기  (0) 2021.07.29
Vivaldi Browser 사용기  (0) 2021.07.02
Heroku 기본 사용법  (0) 2021.06.14
HEROKU salesforce authenticato 새 디바이스에서 인식 안된 문제  (0) 2021.06.14

댓글