KDT/TIL

9/14 TIL : ๊ฒŒ์‹œํŒ ๊ธฐ๋Šฅ์— MongoDB ์ ์šฉ, ์„œ๋ฒ„ ๋ฐฐํฌ

ebulsok 2022. 9. 20. 17:39

๐Ÿ”Ž url vs uri

  • URL(Uniform Resource Location): ์ฃผ์†Œ๊นŒ์ง€๋งŒ ์•Œ๋ ค์คŒ
  • URI(Uniform Resource Identifier): ์ฃผ์†Œ+ํŒŒ์ผ๋ช…๊นŒ์ง€ ํ‘œ์‹œ

 

๐Ÿ”Ž async/await ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ board.js์— ์ฝ”๋“œ ์ ์šฉ + ๋ชจ๋“ˆํ™”

/routes/mongo.js

// @ts-check
const { MongoClient, ServerApiVersion } = require('mongodb');

const uri =
  'mongodb+srv://ebulsok:<password>@cluster0.mhxf9lp.mongodb.net/?retryWrites=true&w=majority';

const client = new MongoClient(uri, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  serverApi: ServerApiVersion.v1,
});

module.exports = client;

board.js

// @ts-check
const express = require('express');

const router = express.Router();

const mongoClient = require('./mongo');

// ๊ธ€ ๋ชฉ๋ก
router.get('/', async (req, res) => {
  const client = await mongoClient.connect();
  const cursor = client.db('KDT-1').collection('board');
  const ARTICLE = await cursor.find({}).toArray();

  const articleLen = ARTICLE.length;
  res.render('board', {
    ARTICLE,
    articleCounts: articleLen,
  });
});

// ๊ธ€ ๋“ฑ๋ก
router.get('/write', (req, res) => {
  res.render('board_write');
});

router.post('/write', async (req, res) => {
  if (req.body.title && req.body.content) {
    const client = await mongoClient.connect();
    const cursor = client.db('KDT-1').collection('board');

    let postID = 1;
    const articleCount = await cursor.count();
    if (articleCount > 0) {
      const lastArticle = await cursor.findOne({}, { sort: { $natural: -1 } });
      postID = lastArticle.postID + 1;
    }

    const newArticle = {
      title: req.body.title,
      content: req.body.content,
      postID: postID,
    };

    await cursor.insertOne(newArticle);
    res.redirect('/board');
  } else {
    const err = new Error('Unexpected form data');
    err.statusCode = 404;
    throw err;
  }
});

// ๊ธ€ ์ˆ˜์ •
router.get('/edit/postID/:postID', async (req, res) => {
  const client = await mongoClient.connect();
  const cursor = client.db('KDT-1').collection('board');

  const editArticle = await cursor.findOne({
    postID: Number(req.params.postID),
  });
  res.render('board_edit', { editArticle });
});

router.post('/edit/postID/:postID', async (req, res) => {
  if (req.body.title && req.body.content) {
    const client = await mongoClient.connect();
    const cursor = client.db('KDT-1').collection('board');

    await cursor.updateOne(
      { postID: Number(req.params.postID) },
      { $set: { title: req.body.title, content: req.body.content } }
    );
    res.redirect('/board');
  } else {
    const err = new Error('Unexpected form data');
    err.statusCode = 404;
    throw err;
  }
});

// ๊ธ€ ์‚ญ์ œ
router.delete('/delete/postID/:postID', async (req, res) => {
  const client = await mongoClient.connect();
  const cursor = client.db('KDT-1').collection('board');

  const result = await cursor.deleteOne({ postID: Number(req.params.postID) });
  if (result.acknowledged) res.send('๊ธ€์ด ์‚ญ์ œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.');
  else {
    const err = new Error('Unexpected form data');
    err.statusCode = 404;
    throw err;
  }
});

module.exports = router;

๐Ÿ”Ž ์„œ๋ฒ„ ๋ฐฐํฌ

  1. aws ํ”„๋ฆฌํ‹ฐ์–ด ํšŒ์›๊ฐ€์ž…
  2. ์„œ๋ฒ„์‹œ๊ฐ„: ์•„์‹œ์•„ ํƒœํ‰์–‘(์„œ์šธ)
  3. ์„œ๋น„์Šค: EC2
  4. ์ธ์Šคํ„ด์Šค ์‹œ์ž‘: Amazon Linux, t2.micro, ํ‚ค ํŽ˜์–ด ์ƒ์„ฑ(RSA, .pem), ๋ณด์•ˆ ๊ทธ๋ฃน ์ƒ์„ฑ
  5. ๋ณด์•ˆ๊ทธ๋ฃน - ์ธ๋ฐ”์šด๋“œ ๊ทœ์น™ ํŽธ์ง‘ - ๊ทœ์น™ ์ถ”๊ฐ€: ํฌํŠธ๋ฒ”์œ„ 4000, ์†Œ์Šค 0.0.0.0/0
  6. ํผ๋ธ”๋ฆญ IPv4 ์ฃผ์†Œ ๋ณต์‚ฌ
  7. mongoDB์—์„œ IP access list์— ์ฃผ์†Œ ์ถ”๊ฐ€
  8. [ํ„ฐ๋ฏธ๋„] ํ‚ค ํŽ˜์–ด ์ €์žฅํ•œ ํด๋”๋กœ ์ด๋™
  9. [ํ„ฐ๋ฏธ๋„] chmod 400 ํ‚คํŽ˜์–ด์ด๋ฆ„.pem
    • chmod: change mode (๋‚˜ | ๊ทธ๋ฃน | ์ „์ฒด)
    • read: 4, write: 2, execute: 1 ์˜ ํ•ฉ์œผ๋กœ ๊ถŒํ•œ ํ‘œ๊ธฐ
    • 400: ๋‚˜ํ•œํ…Œ๋งŒ ์ฝ๊ธฐ ๊ถŒํ•œ, 754: ๋‚˜๋Š” ์ฝ๊ธฐ+์“ฐ๊ธฐ+์‹คํ–‰, ๊ทธ๋ฃน์€ ์ฝ๊ธฐ+์“ฐ๊ธฐ, ์ „์ฒด๋Š” ์ฝ๊ธฐ
  10. ssh๋ฅผ ์ด์šฉํ•ด ์„œ๋ฒ„ ์ ‘์†: [ํ„ฐ๋ฏธ๋„] ssh -i ํ‚คํŽ˜์–ด์ด๋ฆ„.pem ec2-user@ํผ๋ธ”๋ฆญIP์ฃผ์†Œ => [ํ„ฐ๋ฏธ๋„] yes
  11. EC2์— node.js ์„ค์น˜(https://docs.aws.amazon.com/ko_kr/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html)
  12. EC2์— git ์„ค์น˜: [ํ„ฐ๋ฏธ๋„] sudo yum install git => [ํ„ฐ๋ฏธ๋„] y
  13. ๋ฐฐํฌ์šฉ ํด๋” ์ƒ์„ฑ: [ํ„ฐ๋ฏธ๋„] mkdir app => [ํ„ฐ๋ฏธ๋„] cd app
  14. [ํ„ฐ๋ฏธ๋„] git clone ๋ฐฐํฌํ•˜๋ ค๋Š”๊นƒํ—ˆ๋ธŒ์ฃผ์†Œ
  15. clone๋œ ํด๋”๋กœ ์ด๋™: [ํ„ฐ๋ฏธ๋„] cd ํด๋”๋ช…
  16. node module ์„ค์น˜: [ํ„ฐ๋ฏธ๋„] npm install
  17. ์‹คํ–‰ ํ…Œ์ŠคํŠธ: [ํ„ฐ๋ฏธ๋„] node app.js

 

๐Ÿ”Ž PM2(Process Manager 2): node.js ํ”„๋กœ๊ทธ๋žจ์˜ ํ”„๋กœ์„ธ์Šค ๊ด€๋ฆฌ์ž

ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ฃฝ์–ด๋„ ๋‹ค์‹œ ์‚ด๋ ค์ฃผ๋Š” ์—ญํ•  ๋“ฑ..

  • ์„ค์น˜: [ํ„ฐ๋ฏธ๋„] npm i pm2 -g
  • ํ”„๋กœ์„ธ์Šค ์‹คํ–‰: [ํ„ฐ๋ฏธ๋„] pm2 start app.js
  • ํ”„๋กœ์„ธ์Šค ์ค‘๋‹จ: [ํ„ฐ๋ฏธ๋„] pm2 stop app.js