Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

9시 24분

글 생성, 수정, 삭제 기능 구현 본문

Javascript/Node.js

글 생성, 수정, 삭제 기능 구현

leeeee.yeon 2021. 7. 5. 20:48
else if(pathname === '/create'){
      db.query(`SELECT * FROM topic`, function(error, topics){
        var title = 'Welcome';
        var description = 'Hello, Node.js';
        var list = template.list(topics);
        var html = template.HTML(title, list,
          `
          <form action="/create_process" method="post">
            <p><input type="text" name="title" placeholder="title"></p>
            <p>
              <textarea name="description" placeholder="description"></textarea>
            </p>
            <p>
              <input type="submit">
            </p>
          </form>
        `,
          `<a href="/create">create</a>`
        );

        response.writeHead(200);
        response.end(html);
      });

    }

글 생성 창에서 글 목록 띄우기 성공

 

이제 create_process 부분의 코드를 수정해서 글 생성 시 SQL에 추가, 글 목록에 추가, 작성한 글로 이동할 수 있도록 하자.

else if(pathname === '/create_process'){
      var body = '';
      request.on('data', function(data){
          body = body + data;
      });
      request.on('end', function(){
          var post = qs.parse(body);
          var title = post.title;
          var description = post.description;
          
          db.query(`
            INSERT INTO topic (title, description, created, author)
              VALUES(?, ?, NOW(), ?)`,
              [post.title, post.description, 'lee'],
              function(error, result){
                if(error) throw error;
                response.writeHead(302, {Location: `/?id=${result.insertId}`});
                response.end();
              }
              );
      });
    }

CRUD 중 C를 성공했다!

이제 U를 해보자!

 

else if(pathname === '/update'){
      db.query(`SELECT * FROM topic`, function(error, topics){
        if(error) throw error;
        db.query(`SELECT * FROM topic WHERE id=?`, [queryData.id], function(error2, topic){
          var list = template.list(topics);
          var html = template.HTML(topic[0].title, list,
            `
            <form action="/update_process" method="post">
              <input type="hidden" name="id" value="${topic[0].id}">
              <p><input type="text" name="title" placeholder="title" value="${topic[0].title}"></p>
              <p>
                <textarea name="description" placeholder="description">${topic[0].description}</textarea>
              </p>
              <p>
                <input type="submit">
              </p>
            </form>
            `,
            `<a href="/create">create</a> <a href="/update?id=${topic[0].id}">update</a>`
          );
          response.writeHead(200);
          response.end(html);
        });
      });
    }

글 수정 시 작성했던 내용이 뜨도록 하는데 성공했다.

이제 update_process 부분을 수정하여 글 수정 기능을 이어서 구현해보자.

 

else if(pathname === '/update_process'){
      var body = '';
      request.on('data', function(data){
          body = body + data;
      });
      request.on('end', function(){
          var post = qs.parse(body);
          var id = post.id;
          var title = post.title;
          var description = post.description;

          db.query(`UPDATE topic SET title=?, description=?, author=? WHERE id=?`,
          [title, description, 'lee', id],
          function(error, result){
            response.writeHead(302, {Location: `/?id=${id}`});
            response.end();
          });
      });
    }

글 수정 기능도 구현 완료! 이제 마지막으로 D를 하러 가장

else if(pathname === '/delete_process'){
      var body = '';
      request.on('data', function(data){
          body = body + data;
      });
      request.on('end', function(){
          var post = qs.parse(body);
          var id = post.id;
          var filteredId = path.parse(id).base;

          db.query(`DELETE FROM topic WHERE id=?`, [post.id], function(error, result){
            if(error) throw error;
            response.writeHead(302, {Location: `/`});
            response.end();
          });
      });
    }

아까 작성한 create update 글이 삭제되고 메인 화면으로 돌아오는 것을 볼 수 있다.

 

CRUD 완성 ~