본문 바로가기
프론트엔드/Vue.js

Netlify Serverless Function

by step 1 2022. 1. 14.
반응형

기존 Netlify를 이용하여 웹페이지를 동작하면 개인의 데이터가 보여지는것을 확인할 수 있다.

 

Get방식으로 파라미터가 전부 보여진다.

 

따라서 Function 설정을 따로 해주어야한다.

공식문서 url: Build functions with JavaScript | Netlify Docs

 

Build functions with JavaScript

Netlify builds, deploys, and hosts your frontend. Learn how to get started, find examples, and access documentation for the modern web platform.

docs.netlify.com

 

기존 프로젝트에 폴더 및 파일 생성

1. 루트경로 -> netlify.toml 파일생성

[functions]
  directory = "functions"

2. 루트경로 -> functions 폴더 생성 -> hello.js 파일 생성

exports.handler = async function(event, context) {
  return {
    statusCode: 200,
    // 문자데이터 반환 방법
    //body: 'Hello world'
    
    //객체 데이터 반환 방법
    body: JSON.stringify({
      name: 'HEROPY',
      age: 85,
      email: 'thesecon@gmail.com'
    })
  }
}

 

git hub에 올려두기

1. git add .

2. git commit -m 'Add Netlify functions'

3. git push origin master

 

netlify에 적용되었는지 확인

 

url 접근으로 확인 -> 뒷부분에 .netlify/functions/hello 추가

https://epic-albattani-ceea5b.netlify.app/.netlify/functions/hello

 

반응형