AWS/AWS 기초

REST API란?

2로 접어듦 2023. 2. 20. 11:08

API의 개념

API란 Application Programming Interface의 약자로, protocol, routines, tools for building SW application의 집합이다.
서로 다른 소프트웨어 component끼리의 interaction, data exchange and service를 가능하게 한다. 다시 말해서, 서로 다른 디바이스, 어플리케이션끼리 통합하거나 데이터를 주고받을 수 있게 하는 데 사용되는 interface이다.

"APIs play a critical role in modern software development, enabling developers to build powerful applications quickly and efficiently by reusing existing components and services."

REST API의 개념 및 정의

REST, or Representational State Transfer, is an architectural style for building web-based applications. It is widely used for building APIs (Application Programming Interfaces) that enable communication between different systems and devices.
REST APIs are a type of API that follows the principles of REST architecture. They are designed to be simple, scalable, and easy to use. In this blog post, we’ll dive into what REST is, the principles it follows, and how to build a REST API.

REST 란 무엇인가?

REST is a software architectural style that defines a set of constraints to be used for creating web services. These constraints are intended to make web services more scalable, flexible, and maintainable.

 

즉, REST 는 자원을 기반으로 하여 HTTP method를 통해 자원(Resource)를 처리하도록 설계된 아키텍쳐이다.

HTTP를 통해 Resource를 명시하고, Method(post, get, put, delete)를 통해 CRUD Operation을 적용하는 것.

 

RESTful web services are built around the following key principles:

더보기

1. Client-server architecture

A client-server architecture separates the concerns of the user interface (client) and the data storage (server), allowing for independent development and evolution.

2. Stateless communication

RESTful web services should be stateless, meaning that each request from a client to a server should contain all the necessary information for the server to understand the request. This allows for better scalability and reliability of the web service.

3. Uniform interface

A uniform interface is a key principle of REST, which defines a consistent way of interacting with web resources. This includes the use of HTTP methods (such as GET, POST, PUT, and DELETE) to perform operations on resources, and the use of hypermedia (links) to allow clients to discover and navigate to related resources.

4. Cacheable

RESTful web services should be designed to be cacheable, meaning that responses from the server can be cached by the client for future use. This can improve the performance of the web service by reducing the number of requests made to the server.

5. Layered system

A layered system is one in which different components of the web service are separated into layers, each with its own responsibilities. This allows for better scalability, as different layers can be scaled independently.

6. Code on demand (optional)

RESTful web services can optionally allow for code to be downloaded and executed by clients. This is often used for building web applications that are highly dynamic and interactive.

 


What is a REST API?

A REST API is a web-based API that follows the principles of REST. It allows clients to communicate with a server by making requests to URLs (Uniform Resource Locators) and receiving responses in a standardized format, typically JSON (JavaScript Object Notation) or XML (eXtensible Markup Language).

A REST API typically consists of the following components:

더보기

1. Resources

A resource is a piece of data that can be accessed by a client through a URL. Examples of resources include users, products, and orders.

2. HTTP methods

HTTP methods are used to perform operations on resources. The most common HTTP methods used in RESTful APIs are GET, POST, PUT, and DELETE.

  • GET: Retrieves a resource
  • POST: Creates a new resource
  • PUT: Updates an existing resource
  • DELETE: Deletes a resource

3. Representations

Representations are the data formats used to transmit information between the client and server. JSON and XML are the most commonly used representations in RESTful APIs.

4. Hypermedia (optional)

Hypermedia is the use of links between resources to allow clients to discover and navigate to related resources. This can be used to build more dynamic and interactive APIs.


REST API 코드 예제

const express = require('express');
const app = express();

// Define an array of objects representing users
let users = [
  { id: 1, name: 'Alice', age: 30 },
  { id: 2, name: 'Bob', age: 40 },
  { id: 3, name: 'Charlie', age: 50 }
];

// Define a route for getting all users
app.get('/users', (req, res) => {
  res.json(users);
});

// Define a route for getting a single user by id
app.get('/users/:id', (req, res) => {
  const id = parseInt(req.params.id);
  const user = users.find(user => user.id === id);
  if (user) {
    res.json(user);
  } else {
    res.status(404).send('User not found');
  }
});

// Define a route for creating a new user
app.post('/users', (req, res) => {
  const user = req.body;
  user.id = users.length + 1;
  users.push(user);
  res.status(201).json(user);
});

// Define a route for updating an existing user
app.put('/users/:id', (req, res) => {
  const id = parseInt(req.params.id);
  const user = users.find(user => user.id === id);
  if (user) {
    Object.assign(user, req.body);
    res.json(user);
  } else {
    res.status(404).send('User not found');
  }
});

// Define a route for deleting an existing user
app.delete('/users/:id', (req, res) => {
  const id = parseInt(req.params.id);
  const index = users.findIndex(user => user.id === id);
  if (index >= 0) {
    users.splice(index, 1);
    res.sendStatus(204);
  } else {
    res.status(404).send('User not found');
  }
});

// Start the server
app.listen(3000, () => {
  console.log('Server started on port 3000');
});

In this example, we define an array of objects representing users and define routes for getting all users, getting a single user by id, creating a new user, updating an existing user, and deleting an existing user. Each route corresponds to a particular HTTP method (GET, POST, PUT, DELETE), and returns a response in JSON format.

 

 

참고한 사이트

  1. https://gmlwjd9405.github.io/2018/09/21/rest-and-restful.html