๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒŒ | WEB DEV/NodeJS

[NodeJS+Express] 10. ๊ธ€ ์ž‘์„ฑ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ (MongoDB ๋ฐ์ดํ„ฐ ์‚ฝ์ž…)

by KASSID 2023. 10. 14.

๋ชฉ์ฐจ

    728x90

    ์ด๋ฒˆ์—๋Š” ๊ธ€ ์ž‘์„ฑ ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค์–ด๋ณด์ž!

     

    0. ์‹œ์ž‘ํ•˜๊ธฐ์ „..

    ๋จผ์ € ๊ธ€ ์ž‘์„ฑ๊ธฐ๋Šฅ์„ ๋งŒ๋“œ๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

     

    ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€์„ ์„œ๋ฒ„์— post ์š”์ฒญ์„ ํ•˜๋ฉด

    ์„œ๋ฒ„๋Š” ๊ทธ ๋ฐ์ดํ„ฐ๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ํ˜•์‹์ธ์ง€ ํ™•์ธ์„ ํ•œ ํ›„,

    ์ด์ƒ์ด ์—†๋‹ค๋ฉด DB์— ๋“ฑ๋ก์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ์–ด์•ผํ•  ๊ฒƒ์ด๋‹ค.

    (์œ ์ €๊ฐ€ ์ง์ ‘ DB๋ฅผ ๊ฑด๋“œ๋ฆฌ๋„๋ก ํ•ด์„œ๋Š” ์•ˆ๋  ๊ฒƒ์ด๋‹ค..)

     

    1.  ๊ธ€ ์ž‘์„ฑ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ

    1) ํŒŒ์ผ ์ƒ์„ฑ

    ๊ธ€์„ ์ž‘์„ฑํ•  form์„ ๋‹ด์€ ํŒŒ์ผ์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•œ ๋’ค ์„œ๋ฒ„์— ๋“ฑ๋กํ•ด์ค€๋‹ค.

    //write.ejs
    ...
    <form>
      <textarea></textarea>
      <textarea></textarea>
      <button></button>
    </form>

     

    //server.ejs
    app.get('/write', (request, response) => {
      response.render('write.ejs');
    })

     

    2) ํŒŒ์ผ ์„ค์ •

    ๊ธ€์„ ์ž‘์„ฑํ•ด์„œ ๋ณด๋‚ด๊ธฐ ์œ„ํ•œ ์„ค์ •์„ ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

     

    (1) EJSํŒŒ์ผ

    //write.ejs
    ...
    <form actoin="/addpost" method="POST">
      <textarea name="title"></textarea>
      <textarea name="content"></textarea>
      <button type="submit"></button>
    </form>

     

    form ํƒœ๊ทธ : action๊ณผ method ์†์„ฑ์„ ์‚ฝ์ž…ํ•ด์ค€๋‹ค.

      - method๋Š” DB์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ผ ๊ฒƒ์ด๋ฏ€๋กœ POST๋กœ ์„ค์ •ํ•œ๋‹ค.

      - action์€ ์„œ๋ฒ„์— ์š”์ฒญ์„ ๋ณด๋‚ผ url์„ ์„ค์ •ํ•œ๋‹ค. (์„œ๋ฒ„ํŒŒ์ผ์— ๋™์ผํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์˜ˆ์ •)

     

    textarea, input ๋“ฑ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ํƒœ๊ทธ : name ์†์„ฑ์„ ์‚ฝ์ž…ํ•ด์ค€ ํ›„ ์ž„์˜๋กœ ์ด๋ฆ„์„ ์ž‘์„ฑํ•ด์ค€๋‹ค.

    button ํƒœ๊ทธ : type ์†์„ฑ์— submit์œผ๋กœ ์„ค์ •ํ•˜๋ฉด form์— ์„ค์ •ํ•œ ๋‚ด์šฉ์„ ์ œ์ถœํ•˜๋„๋ก ์„ค์ •ํ•œ๋‹ค.

     

    (2) ์„œ๋ฒ„ ํŒŒ์ผ

    //server.js
    app.post('/addpost', (request, response)=>{
      console.log(request.body);
    })

    ์œ„์ฒ˜๋Ÿผ app.post()๋ฅผ ํ†ตํ•ด form์—์„œ ์ž‘์„ฑํ•œ url์„ ๋“ฑ๋กํ•œ๋‹ค.

     

    ์ด๋•Œ request.body์— ํ†ตํ•ด post ์š”์ฒญ์— ์ฒจ๋ถ€ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์žˆ๋Š”๋ฐ

    ์ฝ˜์†”์ฐฝ์—์„œ ๋จผ์ € ํ™•์ธํ•ด๋ณด์ž.

     

    ๊ทธ ์ „์— ์ด๋ฅผ ์œ„ํ•ด์„œ ํ•œ๊ฐ€์ง€ ์„ค์ •์ด ํ•„์š”ํ•˜๋‹ค!

     

    //server.js
    app.use(express.json())
    app.use(express.urlencoded({extended:true}))

    ์œ„ ์ฝ”๋“œ๋ฅผ ์„œ๋ฒ„ํŒŒ์ผ ์ƒ๋‹จ์— ์ž‘์„ฑํ•ด์ค€๋‹ค.

     

     

    ์ด์ œ ๊ธ€์„ ์ž‘์„ฑํ•œ ํ›„ ์ œ์ถœ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ด๋ณด๋ฉด

    ์ž‘์„ฑํ•œ ๋‚ด์šฉ๋“ค์ด {name:์ž‘์„ฑ๋‚ด์šฉ, ... }์˜ ํ˜•์‹์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

     

     

    3) ๊ธ€ ๋“ฑ๋กํ•˜๊ธฐ

    ์ด์ œ ๋ณธ๊ฒฉ์ ์œผ๋กœ ๊ธ€์„ ๋“ฑ๋กํ•˜๊ธฐ ์ „์— ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•  DB๋ฅผ ์‚ดํŽด๋ณด์ž.

    ์ด ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” MongoDB๋ฅผ ์‚ดํŽด๋ณด๋ฉด

    forum ์ด๋ผ๋Š” DB๋ช…์— post ๋ผ๋Š” collection๋ช…์„ ์ด์ „ ํฌ์ŠคํŠธ์—์„œ ์„ค์ •ํ•ด๋†“์•˜๋‹ค.

     

    ๋˜ํ•œ ํ…Œ์ŠคํŠธ๋กœ ๋„ฃ์—ˆ๋˜ ๋ฐ์ดํ„ฐ๋ธ”๋ก๋„ title, content๋ผ๋Š” key๋ฅผ ๊ฐ€์ง„ ํ˜•ํƒœ์ด๋‹ค.

     

    ์ด๋ฅผ ๊ทธ๋Œ€๋กœ ์ด์šฉํ•ด๋ณด์ž.

     

    //server.js
    
    let db;
    const url = 'MongoDB URL'
    new MongoClient(url).connect().then((client)=>{
      console.log("DB connected");
      db = client.db('forum') //์„ค์ •ํ•œ db ์ด๋ฆ„
    
      app.listen(8080, () => {
        console.log('http://localhost:8080 ์—์„œ ์„œ๋ฒ„ ์‹คํ–‰์ค‘!')
      })
    
    }).catch((err)=>{
      console.error(err);
    })
    
    ...
    
    app.post('/newpost', async(request, response)=>{
      await db.collection('post').insertOne({title:request.body.name, content:request.body.content});
      response.redirect('/list')
    })

     

    ์œ„์—์„œ ํ™•์ธํ–ˆ๋˜ requset.body๋กœ๋ถ€ํ„ฐ ๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ ์ „๋‹ฌํ•ด์ฃผ์—ˆ๋‹ค.

    DB์— ๊ฐ’์„ ์ „๋‹ฌํ•ด๋•Œ์—๋„ ์—ญ์‹œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•˜์—ฌ async await๋ฅผ ์‚ฌ์šฉํ•ด์ค€๋‹ค.

     

    ์ดํ›„, ์„œ๋ฒ„ ํŒŒ์ผ ๋‚ด์— ์ •์˜ ๋˜์—ˆ๋˜ db ๋ณ€์ˆ˜์— .collection('collection๋ช…')์— insertOne()ํ•จ์ˆ˜๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค.

    ๋˜ํ•œ response.redirect('path๋ช…')์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ์ €์žฅ ํ›„ url ์ ‘์†์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

     

     

    ์ด์ œ form์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•œํ›„ submit ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด

     

    ์•„๋ž˜์™€ ๊ฐ™์ด DB์— ์ž˜ ์ „๋‹ฌ๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

     

    4) ๊ธ€ ๋“ฑ๋ก ์ „ ์„œ๋ฒ„ ๊ฒ€์ฆ๋‹จ๊ณ„ ์ถ”๊ฐ€ํ•˜๊ธฐ

    ๊ธ€ ์‹œ์ž‘์— ๋‹ค๋ฃจ์—ˆ๋˜

    1. ํด๋ผ์ด์–ธํŠธ์˜post ์š”์ฒญ / 2. ์„œ๋ฒ„๋Š” ๊ทธ ๋ฐ์ดํ„ฐ๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ํ˜•์‹์ธ์ง€ ํ™•์ธ / 3. ์ด์ƒ์ด ์—†๋‹ค๋ฉด DB์— ๋“ฑ๋ก

     

    ์ด ์ค‘ 2๋ฒˆ, ์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ ๊ฒ€์ฆ์„ ํ•˜๋Š” ๊ณผ์ •์„ ๊ตฌํ˜„ํ•ด๋ณด์ž.

     

    [๊ตฌํ˜„ํ•  ๊ฒ€์ฆ ๋‚ด์šฉ]
    1. ์ œ๋ชฉ์ด ๋น„์–ด์žˆ๋Š” ๊ฒฝ์šฐ
    2. ์ œ๋ชฉ 30์ž ์ด๋‚ด
    //server.js
    app.post('/newpost', async(request, response)=>{
      try{
        if(request.body.title == ""){
          response.send("์ œ๋ชฉ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”");
        }
        else if(request.body.title.length > 30){
          response.send("์ œ๋ชฉ์„ 30์ž ์ด๋‚ด๋กœ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");
        }
        else{
          await db.collection('post').insertOne({title:request.body.title, content:request.body.content});
          response.redirect('/list');
        }
      }catch(err){
        console.log(err);
        response.send("DB์˜ค๋ฅ˜");
        //500์ฝ”๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ๋•Œ๋ฌธ์—->response.status(500).send("DB์—๋Ÿฌ");
      }
    })

    - try, catch : DB์— ์—ฐ๊ฒฐ์ด ๋˜์ง€ ์•Š๊ฑฐ๋‚˜, _id๊ฐ€ ๋˜‘๊ฐ™์•„์„œ ์ €์žฅ์ด ๋ถˆ๊ฐ€ํ•˜๋Š” ๋“ฑ์˜ ์—๋Ÿฌ์— ๋Œ€ํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

    - if else ์ œ์–ด๋ฌธ : ์กฐ๊ฑด๋ฌธ์„ ํ†ตํ•ด ์œ ์ €์—๊ฒŒ ์˜ค๋ฅ˜ ๋‚ด์šฉ์„ ๋ช…์‹œํ•ด์ฃผ์—ˆ๋‹ค!

     

     

     

    [์š”์•ฝ]
    - <form>ํƒœ๊ทธ๋กœ post ์š”์ฒญ
    - request.body์— post ์š”์ฒญํ•œ ๋‚ด์šฉ ๋“ค์–ด์žˆ์Œ
    - try, catch, ์กฐ๊ฑด๋ฌธ ๋“ฑ ํ™œ์šฉํ•˜์—ฌ ์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ ๊ฒ€์ฆ
    - .insertOne() ์œผ๋กœ ๋ฐ์ดํ„ฐ DB์— ์‚ฝ์ž…

     

    ๋Œ“๊ธ€