๊ฐœ๋ฐœ Code/์›น๊ฐœ๋ฐœ WebDev

[WebDev][Node.js] Node.js๋กœ REST API ๊ฐœ๋ฐœํ•˜๊ธฐ (CRUD)

5hr1rnp 2025. 1. 31. 21:16
๋ฐ˜์‘ํ˜•

2025.01.30 - [๊ฐœ๋ฐœ Code/์›น๊ฐœ๋ฐœ WebDev] - [WebDev][Node.js] Node.jsํ™˜๊ฒฝ ์„ค์ • ๋ฐ Express.js๋ฅผ ํ™œ์šฉํ•œ ์„œ๋ฒ„ ๋งŒ๋“ค๊ธฐ

1. REST API๋ž€?


  • REST(Representational State Transfer) API๋Š” HTTP ํ”„๋กœํ† ์ฝœ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›๋Š” ๋ฐฉ์‹์ด๋‹ค.
  • CRUD(Create, Read, Update, Delete) ๊ธฐ๋Šฅ์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

2. Express.js๋ฅผ ํ™œ์šฉํ•œ REST API ๊ตฌ์ถ•


2.1. ํ”„๋กœ์ ํŠธ ์ดˆ๊ธฐํ™” ๋ฐ ํ•„์ˆ˜ ํŒจํ‚ค์ง€ ์„ค์น˜

  • ์ƒˆ๋กœ์šด ํ”„๋กœ์ ํŠธ ํด๋”๋ฅผ ์ƒ์„ฑํ•˜๊ณ  VSCode์—์„œ ํ•ด๋‹น ํด๋”๋กœ ์ด๋™ํ•จ:
mkdir my-rest-api
cd my-rest-api
  • npm์„ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๋ฅผ ์ดˆ๊ธฐํ™”ํ•จ:
npm init -y
  • Express.js ๋ฐ ๊ธฐํƒ€ ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•จ:
npm install express body-parser cors

2.2. ๊ธฐ๋ณธ ์„œ๋ฒ„ ์„ค์ •

  • server.js ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด Express ์„œ๋ฒ„๋ฅผ ์„ค์ •ํ•จ:
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');

const app = express();
const PORT = 3000;

app.use(cors());
app.use(bodyParser.json());

app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});
728x90
๋ฐ˜์‘ํ˜•

2.3. RESTful API ์—”๋“œํฌ์ธํŠธ ๊ตฌํ˜„ (CRUD ๊ธฐ๋Šฅ)

1) ๋ฐ์ดํ„ฐ ์ €์žฅ์†Œ(Mock Data)

const users = [];

2) ์‚ฌ์šฉ์ž ๋ชฉ๋ก ์กฐํšŒ (Read)

app.get('/users', (req, res) => {
    res.json(users);
});

3) ์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž ์ถ”๊ฐ€ (Create)

app.post('/users', (req, res) => {
    const user = req.body;
    users.push(user);
    res.status(201).json(user);
});

4) ์‚ฌ์šฉ์ž ์ •๋ณด ์ˆ˜์ • (Update)

app.put('/users/:id', (req, res) => {
    const { id } = req.params;
    const updatedUser = req.body;
    const index = users.findIndex(user => user.id === parseInt(id));
    if (index !== -1) {
        users[index] = updatedUser;
        res.json(updatedUser);
    } else {
        res.status(404).json({ message: 'User not found' });
    }
});

5) ์‚ฌ์šฉ์ž ์‚ญ์ œ (Delete)

app.delete('/users/:id', (req, res) => {
    const { id } = req.params;
    const index = users.findIndex(user => user.id === parseInt(id));
    if (index !== -1) {
        users.splice(index, 1);
        res.json({ message: 'User deleted' });
    } else {
        res.status(404).json({ message: 'User not found' });
    }
});

3. REST API ํ…Œ์ŠคํŠธ


3.1. Postman์„ ํ™œ์šฉํ•œ API ํ…Œ์ŠคํŠธ

  • Postman ์„ค์น˜: Postman ๊ณต์‹ ์›น์‚ฌ์ดํŠธ์—์„œ ๋‹ค์šด๋กœ๋“œ ํ›„ ์„ค์น˜ํ•จ
  • ์„œ๋ฒ„ ์‹คํ–‰: ํ„ฐ๋ฏธ๋„์—์„œ ๋‹ค์Œ ๋ช…๋ น์–ด ์‹คํ–‰:
node server.js // nodemon server.js
  • GET ์š”์ฒญ ํ…Œ์ŠคํŠธ:
    • Postman์„ ์—ด๊ณ  GET http://localhost:3000/users ์š”์ฒญ์„ ๋ณด๋ƒ„
    • ๋นˆ ๋ฐฐ์—ด [ ] ์ด ์‘๋‹ต์œผ๋กœ ์˜ค๋ฉด ์ •์ƒ ๋™์ž‘
  • POST ์š”์ฒญ ํ…Œ์ŠคํŠธ:
    • Postman์—์„œ POST http://localhost:3000/users ์š”์ฒญ์„ ์„ ํƒ
    • Body ํƒญ์—์„œ raw๋ฅผ ์„ ํƒํ•˜๊ณ  JSON ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ํ›„ Send ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ , ์ถ”๊ฐ€๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์‘๋‹ต์œผ๋กœ ์˜ค๋Š”์ง€ ํ™•์ธ:
{
	id": 1, 
	"name": "5hr1rnp", 
	"email": "5hr1rnp@money.com" 
}
  • PUT ์š”์ฒญ ํ…Œ์ŠคํŠธ:
    • PUT http://localhost:3000/users/1 ์š”์ฒญ์„ ์„ ํƒ.
    • Body์— ๋‹ค์Œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์ „์†ก ํ›„ ๋ณ€๊ฒฝ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์‘๋‹ต์œผ๋กœ ์˜ค๋Š”์ง€ ํ™•์ธ:
{
	"id": 1,
	"name": "5hr1rnp",
	"email": "5hr1rnp@moremoney.com" 
}
  • DELETE ์š”์ฒญ ํ…Œ์ŠคํŠธ:
    • DELETE http://localhost:3000/users/1 ์š”์ฒญ์„ ๋ณด๋ƒ„.
    • ์‘๋‹ต์œผ๋กœ { "message": "User deleted" } ๊ฐ€ ์˜ค๋ฉด ์ •์ƒ ๋™์ž‘ํ•จ.

4. ๋งˆ๋ฌด๋ฆฌ


  • Node.js์™€ Express๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ธฐ๋ณธ์ ์ธ CRUD ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” REST API๋ฅผ ๊ตฌํ˜„ํ•˜์˜€์Œ
  • ์ดํ›„์—๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์—ฐ๋™ํ•˜์—ฌ ๋”์šฑ ํ™•์žฅ๋œ API๋ฅผ ๊ฐœ๋ฐœํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฃฐ ์˜ˆ์ •์ž„
๋ฐ˜์‘ํ˜•