๐ŸŒฑ → ๐ŸŒณ

[node.js] ํ”„๋ ˆ์ž„์›Œํฌ ์—†์ด CRUD ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ ๋ณธ๋ฌธ

Server/Node.js

[node.js] ํ”„๋ ˆ์ž„์›Œํฌ ์—†์ด CRUD ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ

BAY 2022. 8. 27. 15:16
728x90

๐Ÿ“Œ CRUD (Create, Read, Update, Delete)

Create -> post

read -> get

update -> put

delete -> delete

 

๐Ÿ“req, res 

req(uire) : ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋“ค์–ด์˜จ ์š”์ฒญ

res(ponse): ์š”์ฒญ์— ๋”ฐ๋ฅธ ์„œ๋ฒ„์˜ ์‘๋‹ต

 

http ๋”ฐ๋ผ์•ผ ํ•˜๋Š” ๊ทœ์•ฝ

 

๐Ÿ“ nodemon ์‚ฌ์šฉ

"scripts": {
    "server": "nodemon js/main.js",
    "dev": "nodemon --watch \"./js/*.js\" --exec \"npm run server\""
  },

package.json ํŒŒ์ผ ์ˆ˜์ • 

sass ์ฒ˜๋Ÿผ ํŠน์ • ํŒŒ์ผ์—๊ฒŒ --watch ๊ฑธ์–ด ์ค„ ์ˆ˜๋„ ์žˆ์Œ 

๋ณ€ํ™”๊ฐ€ ์ผ์–ด๋‚˜๋ฉด --exec ์˜ต์…˜์œผ๋กœ ๋ช…๋ น์–ด ์‹คํ–‰ ๊ฐ€๋Šฅ 

 

๐Ÿ“ HTTPie

์„œ๋ฒ„์—์„œ post ์š”์ฒญ 

 

โœ… ์ฝ”๋“œ ์ „์ฒด ํ๋ฆ„ 

// @ts-check

const http = require('http');

const posts = [
  {
    id: 1,
    title: '์ฒซ๋ฒˆ์งธ ๋ธ”๋กœ๊ทธ ๊ธ€',
    content: '์ฒซ๋ฒˆ์งธ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค',
  },
  {
    id: 2,
    title: '๋‘๋ฒˆ์งธ ๋ธ”๋กœ๊ทธ ๊ธ€',
    content: '๋‘๋ฒˆ์งธ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค',
  },
];

const server = http.createServer((req, res) => {
  console.log('@@@', req.url);
  // req.url์ด ์กด์žฌํ•˜๋ฉด req.url.split('/'), ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ๋นˆ๋ฐฐ์—ด ์„ ์–ธ
  const urlArr = req.url ? req.url.split('/') : [];
  let id = '-1';
  if (urlArr.length > 2) {
    [, , id] = urlArr;
    // id = urlArr[2];
  }
  // console.log(urlArr);
  // console.log(urlArr[2]);

  /**
   * GET  /posts         ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ
   * GET  /posts/:id     ํŠน์ • ๊ธ€ ๋‚ด์šฉ ๊ฐ€์ ธ์˜ค๊ธฐ
   * POST /posts        ์ƒˆ๋กœ์šด ๊ธ€ ์˜ฌ๋ฆฌ๊ธฐ
   * PUT  /posts/:id     ํŠน์ • ๊ธ€ ๋‚ด์šฉ ์ˆ˜์ •ํ•˜๊ธฐ
   * DELETE /posts/:id  ํŠน์ • ๊ธ€ ์‚ญ์ œํ•˜๊ธฐ
   */
  if (req.url === '/posts' && req.method === 'GET') {
    const result = {
      posts: posts.map((post) => ({
        id: post.id,
        title: post.title,
      })),
      // ์ „์ฒด ๊ธ€ ์ˆ˜ return
      totalCount: posts.length,
    };

    // response header ์„ค์ •
    res.setHeader('Content-Type', 'application/json', 'charset = utf-8');
    res.statusCode = 200;
    res.end(JSON.stringify(result));
    console.log('๋ธ”๋กœ๊ทธ์˜ ๊ธ€ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜ค๋Š” API ์ž…๋‹ˆ๋‹ค.');
  } else if (urlArr[1] === 'posts' && req.method === 'GET') {
    res.statusCode = 200;
    console.log('๋ธ”๋กœ๊ทธ์— ํŠน์ • ๊ธ€ ๋‚ด์šฉ์„ ๋ณด์—ฌ์ฃผ๋Š” API ์ž…๋‹ˆ๋‹ค.');
  } else if (req.url === '/posts' && req.method === 'POST') {
    res.statusCode = 200;
    console.log('๋ธ”๋กœ๊ทธ์˜ ์ƒˆ๋กœ์šด ๊ธ€์„ ์˜ฌ๋ฆด ๋•Œ ํ˜ธ์ถœํ•  API ์ž…๋‹ˆ๋‹ค');
  } else if (urlArr[1] === 'posts' && req.method === 'PUT') {
    res.statusCode = 200;
    console.log('๋ธ”๋กœ๊ทธ์˜ ๊ธ€์„ ์ˆ˜์ • ๋•Œ ํ˜ธ์ถœํ•  API ์ž…๋‹ˆ๋‹ค');
  } else if (urlArr[1] === 'posts' && req.method === 'DELETE') {
    res.statusCode = 200;
    console.log('๋ธ”๋กœ๊ทธ์˜ ๊ธ€์„ ์‚ญ์ œ ๋•Œ ํ˜ธ์ถœํ•  API ์ž…๋‹ˆ๋‹ค');
  } else {
    res.statusCode = 400;
    res.end('Not Found');
    console.log('ํ•ด๋‹น API๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.');
  }

  // ์„œ๋ฒ„๊ฐ€ ์ •์ƒ์ผ ๋•Œ 200์ด๋ผ๋Š” ์ฝ”๋“œ๋ฅผ ๋‚ด๋ณด๋ƒ„
  // res.statusCode = 200;
  // res.end('hello, back-end');
});

const PORT = 4000;

server.listen(PORT, () => {
  console.log(`ํ•ด๋‹น ์„œ๋ฒ„๋Š” ${PORT}๋ฒˆ ํฌํŠธ์—์„œ ์ž‘๋™ ์ค‘์ž…๋‹ˆ๋‹ค.`);
});

 

โœ… Read -> GET ๊ตฌํ˜„ (๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ)

์ œ์ผ ์œ„ ์ฝ”๋“œ์—์„œ GET ๋ถ€๋ถ„ ์ˆ˜์ • 

  if (req.url === '/posts' && req.method === 'GET') {
    const result = {
      posts: posts.map((post) => ({
        id: post.id,
        title: post.title,
      })),
      // posts: posts,
      // ์ „์ฒด ๊ธ€ ์ˆ˜ return
      totalCount: posts.length,
    };
    res.statusCode = 200;
    // JSON ํ˜•ํƒœ๋กœ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ”์„œ ์ „๋‹ฌํ•ด์คŒ
    res.end(JSON.stringify(result));
    console.log('๋ธ”๋กœ๊ทธ์˜ ๊ธ€ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜ค๋Š” API ์ž…๋‹ˆ๋‹ค.');
  }

 

โœ… Read -> GET ๊ตฌํ˜„ (ํŠน์ • id ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ธ€ ๊ฐ€์ ธ์˜ค๊ธฐ)

ํŠน์ • id ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ธ€์˜ ์ •๋ณด๋ฅผ ๋ชจ๋‘ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค 

๋ฐฐ์—ด์˜ find method๋ฅผ ์ด์šฉํ•˜์—ฌ id๊ฐ€ ๊ฒน์น˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์•„์„œ ๋ฐ˜ํ™˜ 

๋‹จ, ํ•ด๋‹น id ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ 404 err ์ถœ๋ ฅ(์˜ˆ์™ธ ์ฒ˜๋ฆฌ)

id ๊ฐ’์„ number๋กœ ํ•˜๊ธฐ๋กœ ํ–ˆ์œผ๋‹ˆ id๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ์ฝ”๋“œ ์ˆ˜์ • 

 

์ œ์ผ ์œ„ ์ฝ”๋“œ์—์„œ GET(else if (id !== -1 && req.method === 'GET')) ๋ถ€๋ถ„ ์ˆ˜์ • 

else if (id !== -1 && req.method === 'GET') {
    const result = posts.find((post) => post.id === id);
    if (result) {
      res.statusCode = 200;
      res.end(JSON.stringify(result));
      console.log('๋ธ”๋กœ๊ทธ์˜ ํŠน์ • ๊ธ€ ๋‚ด์šฉ์„ ๋ณด์—ฌ์ค„ API ์ž…๋‹ˆ๋‹ค');
      console.log(`Post ID ๊ฐ’์€ ${id} ์ž…๋‹ˆ๋‹ค`);
    } else {
      res.statusCode = 404;
      res.end('ํฌ์ŠคํŠธ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.');
      console.log('ํฌ์ŠคํŠธ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.');
    }
  }

 

โœ… Create -> POST ๊ตฌํ˜„

์ œ์ผ ์œ„ ์ฝ”๋“œ์—์„œ POST ๋ถ€๋ถ„ ์ˆ˜์ • 

else if (req.url === '/posts' && req.method === 'POST') {
    req.setEncoding('utf-8');
    req.on('data', (data) => {
      const newPost = JSON.parse(data);
      // posts.push(newPost);
      posts.push({
        id: posts[posts.length - 1].id + 1,
        title: newPost.title,
        content: newPost.content,
      });
    });
    res.statusCode = 200;
    res.end('์ƒˆ๋กœ์šด ๊ธ€์ด ๋“ฑ๋ก ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.');
    console.log('๋ธ”๋กœ๊ทธ์˜ ์ƒˆ๋กœ์šด ๊ธ€์„ ์˜ฌ๋ฆด ๋•Œ ํ˜ธ์ถœํ•  API ์ž…๋‹ˆ๋‹ค');
  }

 

โœ… Update -> PUT ๊ตฌํ˜„(๊ธฐ์กด ํฌ์ŠคํŠธ ์ˆ˜์ •ํ•˜๊ธฐ)

์ œ์ผ ์œ„ ์ฝ”๋“œ์—์„œ PUT ๋ถ€๋ถ„ ์ˆ˜์ •

else if (id !== -1 && req.method === 'PUT') {
    req.setEncoding('utf-8');
    req.on('data', (data) => {
      const updatePost = JSON.parse(data);
      updatePost.id = id;
      posts[id - 1] = updatePost;
    });
    res.statusCode = 200;
    res.end(`์ˆ˜์ • ๋œ ํฌ์ŠคํŠธ์˜ id๋Š” ${id}๋ฒˆ ์ž…๋‹ˆ๋‹ค.`);
    console.log('๋ธ”๋กœ๊ทธ์˜ ๊ธ€์„ ์ˆ˜์ • ๋•Œ ํ˜ธ์ถœํ•  API ์ž…๋‹ˆ๋‹ค');
  }

 

โœ… DELETE ๊ตฌํ˜„(๊ธฐ์กด ํฌ์ŠคํŠธ ์‚ญ์ œํ•˜๊ธฐ)

์ œ์ผ ์œ„ ์ฝ”๋“œ์—์„œ DELETE ๋ถ€๋ถ„ ์ˆ˜์ •

else if (id !== -1 && req.method === 'DELETE') {
    // index id-1์ธ ๊ฐ’ ํ•˜๋‚˜๋งŒ ์ง€์›Œ
    posts.splice(id - 1, 1);
    res.statusCode = 200;
    res.end(`id ${id}๋ฒˆ์ธ ํฌ์ŠคํŠธ๋ฅผ ์‚ญ์ œ ํ–ˆ์Šต๋‹ˆ๋‹ค`);
    console.log('๋ธ”๋กœ๊ทธ์˜ ๊ธ€์„ ์‚ญ์ œ ๋•Œ ํ˜ธ์ถœํ•  API ์ž…๋‹ˆ๋‹ค');
  }

 

โœ… ์˜ˆ์™ธ์ฒ˜๋ฆฌ 

else {
    res.statusCode = 400;
    res.end('Not Found');
    console.log('ํ•ด๋‹น API๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.');
  }

 

ํ”„๋ ˆ์ž„์›Œํฌ์— ๊ฐ์‚ฌํ•จ.... ใ…Žใ…Ž...

728x90