언어&프레임워크/Node.js

mysql로 글생성기능 구현

밍풀 2023. 1. 28. 19:50

create
update

수정, 추가 페이지가 불완전함

 

create페이지, create한 후 (리다이렉션한) 페이지 만들기 

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
32
33
34
35
36
37
38
39
40
41
 else if (pathname === '/create') {
    db.query('SELECT * FROM topic', function (error, topics) {
      var title = 'Create';
      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);
 
    });
    
  } else if (pathname === '/create_process') {
    var body = '';
    request.on('data', function (data) {
      body = body + data;
    });
    request.on('end', function () {
      var post = qs.parse(body);
      db.query(`
            INSERT INTO topic (title, description, created, author_id) 
              VALUES(?, ?, NOW(), ?)`,
            [post.title, post.description, 1], //일단 author_id자리에 1
            function(error, result){
              if(error){
                throw error;
              }
              response.writeHead(302, {Location: `/?id=${result.insertId}`});//만들어진 행의 id
              response.end();
            }
          )
    });
  } 
cs

수정된 create페이지, 제출 누른후 리다이렉션된 페이지