Nodejs

REST API

마손리 2022. 12. 26. 13:16

API 란?

API란 Aplication Programming Interface의 약자로 어플리캐이션들이나 다른 장치들간의 통신을 위한 규칙을 정하는 것이며 웹 API는 클라이언트와 제공되는 리소스와의 게이트웨이라 할수있다.

 

REST 란?

REST(Representational State Transfer)는 높은 유연성과 자율성을 가진 아키텍처 중에 하나이다. 쉽게 말해 여러 스타일의 아키텍처들이 있지만 위의 장점들로 인해 쉽게 사용이 가능하여 API를 만들때 가장 보편적으로 사용하게 되는 아키텍처 스타일 입니다.

 

REST의 설계 원칙

  1. Uniform interface : 하나의 URI에서의 리소스 요청의 결과는 출처와 관계없이 항상 같아야 한다.
  2. Client-server decoupling : 클라이언트와 서버, 두 어플리캐이션은 완전히 독립적이어야한다. 클라이언트는 URI만을 가지고 서버와 통신하며 서버는 그 URI에 맞는 데이터만을 전송한다. 
  3. Statelessness : 서버는 클라이언트의 모든 요청을 완료하는 통신 방법을 사용한다. 즉 클라이언트의 요청이 완료되면 state가 계속 연결된 상태가아닌 stateless상태가 된다.
  4. Cacheability : 서버의 응답 시간을 개선하기위해 클라이언트와 서버는 일부 응답을 저장하는 프로세스인 캐싱을 지원한다.
  5. Layered system architecture : 서버는 클라이언트의 요청을 다른 서버로 전달할수 있지만 응답은 처음 요청을 받은 서버에서 해주며 클라이언트가 그 과정을 알수 없도록 설계해야 한다.
  6. Code ondemand : 서버는 필요해 의해 클라이언트에 작동 가능한 코드를 응답할수도 있다.

 

REST API의 작동방식

REST API는 HTTP method를 통해 creating, reading, updating, deleting과 같은 작업들을 수행한다. 

클라이언트가 서버에 요청(request)하면 서버가 그 요청에 맞는 작업을 수행한뒤 클라이언트로 다시 응답(response)하게되고 그이후 stateless 상태가 된다.

 

(출처1:https://www.ibm.com/topics/rest-apis#:~:text=An%20API%2C%20or%20application%20programming,representational%20state%20transfer%20architectural%20style.)

(출처2:https://aws.amazon.com/ko/what-is/restful-api/)

 

 

REST API 구현

import express from "express";
import bodyParser from "body-parser";

const app = express();
app.use(bodyParser.json()); // req.body를 접근할수 있게 해주는 미들웨어

interface DB {
  id: number;
  name: string;
  age: number;
}

class RestPractice {		// DB 대용으로 만든 클래스
  private db: DB[] = [];
  private id: number = 1;
  constructor() {}
  
  create(name: string, age: number) {
    const newUser: DB = { id: this.id, name, age };
    this.db.push(newUser);
    this.id++;
    return newUser;
  }
  read() {
    return this.db;
  }
  update(id: number, name?: string, age?: number) {
    this.db.map((user) => {
      if (user.id === id) {
        if (name) {
          user.name = name;
        }
        if (age) {
          user.age = age;
        }
      }
      return user;
    });
    return this.db.find((user) => user.id === id);
  }
  remove(id: number) {
    this.db = this.db.filter((user) => user.id !== id);
  }
}

const restApi = new RestPractice();

app.listen(4000, () => console.log("server connected http://localhost:4000"));

일단 위와 같이 express 서버와 DB를 대신할 클래스를 하나 만들어 주었다.

 

 

interface Request {
  body: {
    id?: number;
    name?: string;
    age?: number;
  };
}

app.get("/", function (req: Request, res) {		// GET method, read역활
  const users = restApi.read();
  res.send({ ok: true, users });
});

app.post("/", function (req: Request, res) {	// POST method, create역활
  const {
    body: { name, age },
  } = req;
  const createUser = restApi.create(name, age);
  res.send({ ok: true, createUser });
});

app.patch("/", function (req: Request, res) {	// PATCH method, update역활
  const {
    body: { id, name, age },
  } = req;
  const updateUser = restApi.update(id, name, age);
  res.send({ ok: true, updateUser });
});

app.delete("/", function (req: Request, res) {	// DELETE method, delete역활
  const {
    body: { id },
  } = req;
  restApi.remove(id);
  res.send({ ok: true });
});

이후 get, post, patch, delete을 이용해서 간단하게 유저관리가 가능한 CRUD API 서버를 만들수 있었다.

 

 

(관련 깃헙: https://github.com/Mason3144/booking-manager-backend/blob/master/src/routers/productRouter.ts)

마무리, PUT 과 PATCH에 관해...

보통 PUT은 하나의 객체 데이터 전체를, PATCH는 하나의 객체 데이터 중 몇몇가지만을 업데이트 할때 사용한다고 알고 있다.

여러차례 실험을 해보니 들어오는 데이터와 결과값이 PUT과 PATCH 모두 같았다. 그래서 3시간정도 자료를 수집한 결과, 모두 ORM사용을 했을때 우리가 흔히 알고있는 것처럼 PUT은 데이터 전체를, PATCH는 원하는 데이터만을 수정하는것을 발견했다.

결국 PUT과 PATCH 중 어떤것을 쓸지는 ORM을 쓰든 안쓰든 개발자가 작성한 코드의 로직에 의해 결정 되어야 한다는 것으로 결론을 지었다.

 

'Nodejs' 카테고리의 다른 글

GraphQL API  (0) 2022.12.29
GraphQL에서 파일 업로드  (0) 2022.12.24
GraphQl과 Apollo  (1) 2022.12.23
AWS-s3, multer, multer-s3를 이용한 express 파일업로드  (0) 2022.12.23
Express 서버에 Mysql과 session store 설치(AWS-RDS이용)  (0) 2022.12.14