https://docs.github.com/en/rest?apiVersion=2022-11-28
GitHub REST API documentation - GitHub Docs
To create integrations, retrieve data, and automate your workflows, build with the GitHub REST API.
docs.github.com
github์์ ์ ๊ณตํ๋ api ๋ชฉ๋ก๋ค์

์ ์ผ ํ๋จ์ Users api๋ฅผ ํ์ฉํ ์์

ํ ๋ช ์ ์ ์ ๋ฅผ ์ฐพ์์ด


์ด api๋ฅผ ์์ฒญ ํ ๋
fetch api
Axios ์ฌ์ฉํด๋ณผ๊ฑฐ์
Axios
๋ธ๋ผ์ฐ์ ์ node.js๋ฅผ ์ํ Promise ๊ธฐ๋ฐ HTTP ํด๋ผ์ด์ธํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ Axios๋ ๋ธ๋ผ์ฐ์ ์ node.js๋ฅผ ์ํ ๊ฐ๋จํ Promise ๊ธฐ๋ฐ HTTP ํด๋ผ์ด์ธํธ์ ๋๋ค. Axios๋ ํ์ฅ ๊ฐ๋ฅํ ์ธํฐํ์ด์ค๋ฅผ ๊ฐ์ง ์์ ํจํค์ง๋ก
axios-http.com
ajax๋ฅผ ์ข ๋ ํธํ๊ฒ ํ๊ธฐ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์

gitbash ๋ก ์ด๊ธฐ

code . vs์ฝ๋๋ก ์ผ

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>๊นํ๋ธ ์ ์ ์ฐพ๊ธฐ</title>
</head>
<body>
<form class="user-form" id="form">
<input
type="text"
id="search"
placeholder="๊นํ๋ธ ์ ์ ๊ฒ์"
autocomplete="off"
/>
</form>
<main id="main">
<div class="card">
<div>
<!-- ์ธ์คํ๋์ฌ์์ ๋๋ค์ผ๋ก ์ด๋ฏธ์ง ๊ฐ์ ธ์ด -->
<img
src="https://source.unsplash.com/random/300×300"
class="avatar"
/>
</div>
<div class="user-info">
<h2>ํ๊ธธ๋</h2>
<p>lorem15</p>
<ul>
<li>300 <strong>Fallowers</strong></li>
<li>100 <strong>Fallowing</strong></li>
<li>30 <strong>Repos</strong></li>
</ul>
<div class="repos">
<a href="#" class="repo">์ ์ฅ์ 1</a>
<a href="#" class="repo">์ ์ฅ์ 2</a>
<a href="#" class="repo">์ ์ฅ์ 3</a>
</div>
</div>
</div>
</main>
</body>
</html>

css์์ฑ

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;400&display=swap');
* {
box-sizing: border-box;
}
body {
background-color: cornflowerblue;
color: #fff;
font-family: 'Poppins', sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
overflow: hidden;
margin: 0;
}
.user-form {
width: 100%;
max-width: 700px;
}
.user-form input {
width: 100%;
display: block;
background-color: #4c2885;
border: none;
border-radius: 10px;
color: #fff;
padding: 1rem;
margin-bottom: 2rem;
font-family: inherit;
font-size: 1rem;
box-shadow: 0 5px 10px rgba(154, 160, 185, 0.05), 0 15px 40px rgba(0, 0, 0, 0.1);
}
.user-form input::placeholder {
color: #bbb;
}
.user-form input:focus {
outline: none;
}
.card {
max-width: 800px;
background-color: #4c2885;
border-radius: 20px;
box-shadow: 0 5px 10px rgba(154, 160, 185, 0.05), 0 15px 40px rgba(0, 0, 0, 0.1);
display: flex;
padding: 3rem;
margin: 0 1.5rem;
}
.avatar {
border-radius: 50%;
border: 10px solid #2a2a72;
height: 150px;
width: 150px;
}
.user-info {
color: #eee;
margin-left: 2rem;
}
.user-info h2 {
margin-top: 0;
}
.user-info ul {
list-style-type: none;
display: flex;
justify-content: space-between;
padding: 0;
max-width: 400px;
}
.user-info ul li {
display: flex;
align-items: center;
}
.user-info ul li strong {
font-size: 0.9rem;
margin-left: 0.5rem;
}
.repo {
text-decoration: none;
color: #fff;
background-color: #212a72;
font-size: 0.7rem;
padding: 0.25rem 0.5rem;
margin-right: 0.5rem;
margin-bottom: 0.5rem;
display: inline-block;
}
@media (max-width: 500px) {
.card {
flex-direction: column;
align-items: center;
}
.user-form {
max-width: 400px;
}
}

lorem15 ์ฌ์ฉํด์ ๋๋ค ๋จ์ด ๋ฃ์ด์ฃผ๊ณ
๋๋ ์์์ ๋ฐ๊ฟ์ค

https://axios-http.com/kr/docs/intro
์์ํ๊ธฐ | Axios Docs
์์ํ๊ธฐ ๋ธ๋ผ์ฐ์ ์ node.js์์ ์ฌ์ฉํ ์ ์๋ Promise ๊ธฐ๋ฐ HTTP ํด๋ผ์ด์ธํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ Axios๋? Axios๋ node.js์ ๋ธ๋ผ์ฐ์ ๋ฅผ ์ํ Promise ๊ธฐ๋ฐ HTTP ํด๋ผ์ด์ธํธ ์ ๋๋ค. ๊ทธ๊ฒ์ ๋ํ ์ ๋๋ค(๋์ผํ ์ฝ
axios-http.com

Axios ๋ผ์ด๋ธ๋ฌ๋ฆฌ CDN ์ถ๊ฐํ๊ธฐ
endpoint


์์ ์ฃผ์๋ก ๊ฐ์ ธ์ฌ๊ฑฐ์

์ผ๋จ ํ ์คํธํด์ค


endpoint๋ฅผ ์ฌ์ฉํด์ ์ ์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ๊ฑด๋ฐ


๊ธฐ๋ณธ์ ์ผ๋ก ๋ค์ด๊ฐ๋ api ๋ฒก์ค๋ ์ฃผ์์
localhost:8080๊ณผ ๋์ผํ๋ค๊ณ ์๊ฐํ๋ฉด๋จ
const API_URL = "http://api.github.com/users/";
async function getUser(username) {
const result = await axios(API_URL + username);
console.log(result);
}
getUser("jbkim08"); //ํด๋น ์ ์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ด
console.log๋ก result๋ฅผ ์ถ๋ ฅํด๋ด
f12 ๊ฐ๋ฐ์๋ชจ๋๋ก ํ์ธํด๋ณด๋ฉด ์ฝ์์ ๋ธ

status 200 ์ ์์ ์ผ๋ก ์ ๋ณด๋ฅผ ๋ฐ์
์ฐ๋ฆฌ๊ฐ ์ํ๋ ์ ๋ณด๋ data๋ง ์์ผ๋ฉด ๋จ

๊นํ๋ธ ์ ์ ์ ๋ธ๋ก๊ทธ, ๊ฐ์ ๋ ์ง ๋ฑ ์์

์๋ฐ ์คํฌ๋ฆฝํธ ์์
data๋ง ๊ฐ์ ธ์ค๊ธฐ
async function getUser(username) {
const { data } = await axios(API_URL + username);
console.log(data);
}
๋ค์ ํ์ธํ๊ธฐ data ๋ง ๊ฐ์ ธ์ด

๋ง์ฝ ์์ด๋๋ฅผ ์ ๋ชป ์ ๋ ฅํ๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํจ

๊ทธ๋์ try catch๋ฌธ์ผ๋ก ์๋ฌ๊ฐ ์์ผ๋ฉด ์๋ฌ ์ถ๋ ฅํ๊ฒ๋ ์ฝ๋ ์์ ํจ
async function getUser(username) {
try {
const { data } = await axios(API_URL + username);
console.log(data);
} catch (error) {
if (error) {
console.log(error);
}
}
}
์๋์ ๊ฐ์ด ์๋ฌ๊ฐ ์ถ๋ ฅ๋๋ค.



์์ด๋ ์ ๋ ฅ์ฐฝ์ ์์ด๋๋ฅผ ์ ๋ ฅํ๊ณ ์ํฐ๋ฅผ ๋๋ฅด๋ฉด submit๋จ
ํ ์คํธํด๋ด
const form = document.getElementById("form");
form.addEventListener("submit", (e) => {
e.preventDefault(); //
console.log("์๋ธ๋ฐ์ด๋ฒคํธ");
});

const form = document.getElementById("form");
const search = document.getElementById("search");
form.addEventListener("submit", (e) => {
e.preventDefault(); // submit ์ ์๋ฒ ์ ๋ฌ ์ด๋ฒคํธ ์ค์ง
const user = search.value;
console.log(user);
});
ํ ์คํธ

//alert("ํ
์คํธ");
//์์๋ผ์ ๋๋ฌธ์๋ก ์์ฑํจ
const API_URL = "http://api.github.com/users/";
async function getUser(username) {
try {
const { data } = await axios(API_URL + username);
console.log(data);
} catch (error) {
if (error) {
console.log(error);
}
}
}
//getUser("jbkim08"); //ํด๋น ์ ์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ด
const form = document.getElementById("form");
const search = document.getElementById("search");
form.addEventListener("submit", (e) => {
e.preventDefault(); // submit ์ ์๋ฒ ์ ๋ฌ ์ด๋ฒคํธ ์ค์ง
const user = search.value;
//console.log(user);
if (user) {
getUser(user); //์ ์ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
search.value = ""; // ์
๋ ฅ๊ฐ ์ด๊ธฐํ
}
});
ํ์ธ

'FRONTEND > ๊ธฐํ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Typescript] ๊ณต๋ถ ๋ด์ฉ ์ ๋ฆฌ (1) | 2024.08.30 |
|---|---|
| (APIํ์ฉ) ๊นํ๋ธ ์ ์ ์ฐพ๊ธฐ app 2 (1) | 2023.11.14 |
| AJAX ๊ธฐ๋ณธ ๊ฐ๋ ๋ฐ ์์ (1) | 2023.10.16 |
| ๋ก๋ ๋ฒํธ ๊ฐ์ ธ์ค๊ธฐ (0) | 2023.10.06 |
| jQuery ๋ฅผ ํ์ฉํด AJAX ์ฌ์ฉ load() (1) | 2023.10.06 |