๐ 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;
๐ ์๋ฒ ๋ฐฐํฌ
- aws ํ๋ฆฌํฐ์ด ํ์๊ฐ์
- ์๋ฒ์๊ฐ: ์์์ ํํ์(์์ธ)
- ์๋น์ค: EC2
- ์ธ์คํด์ค ์์: Amazon Linux, t2.micro, ํค ํ์ด ์์ฑ(RSA, .pem), ๋ณด์ ๊ทธ๋ฃน ์์ฑ
- ๋ณด์๊ทธ๋ฃน - ์ธ๋ฐ์ด๋ ๊ท์น ํธ์ง - ๊ท์น ์ถ๊ฐ: ํฌํธ๋ฒ์ 4000, ์์ค 0.0.0.0/0
- ํผ๋ธ๋ฆญ IPv4 ์ฃผ์ ๋ณต์ฌ
- mongoDB์์ IP access list์ ์ฃผ์ ์ถ๊ฐ
- [ํฐ๋ฏธ๋] ํค ํ์ด ์ ์ฅํ ํด๋๋ก ์ด๋
- [ํฐ๋ฏธ๋] chmod 400 ํคํ์ด์ด๋ฆ.pem
- chmod: change mode (๋ | ๊ทธ๋ฃน | ์ ์ฒด)
- read: 4, write: 2, execute: 1 ์ ํฉ์ผ๋ก ๊ถํ ํ๊ธฐ
- 400: ๋ํํ ๋ง ์ฝ๊ธฐ ๊ถํ, 754: ๋๋ ์ฝ๊ธฐ+์ฐ๊ธฐ+์คํ, ๊ทธ๋ฃน์ ์ฝ๊ธฐ+์ฐ๊ธฐ, ์ ์ฒด๋ ์ฝ๊ธฐ
- ssh๋ฅผ ์ด์ฉํด ์๋ฒ ์ ์: [ํฐ๋ฏธ๋] ssh -i ํคํ์ด์ด๋ฆ.pem ec2-user@ํผ๋ธ๋ฆญIP์ฃผ์ => [ํฐ๋ฏธ๋] yes
- EC2์ node.js ์ค์น(https://docs.aws.amazon.com/ko_kr/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html)
- EC2์ git ์ค์น: [ํฐ๋ฏธ๋] sudo yum install git => [ํฐ๋ฏธ๋] y
- ๋ฐฐํฌ์ฉ ํด๋ ์์ฑ: [ํฐ๋ฏธ๋] mkdir app => [ํฐ๋ฏธ๋] cd app
- [ํฐ๋ฏธ๋] git clone ๋ฐฐํฌํ๋ ค๋๊นํ๋ธ์ฃผ์
- clone๋ ํด๋๋ก ์ด๋: [ํฐ๋ฏธ๋] cd ํด๋๋ช
- node module ์ค์น: [ํฐ๋ฏธ๋] npm install
- ์คํ ํ ์คํธ: [ํฐ๋ฏธ๋] node app.js
๐ PM2(Process Manager 2): node.js ํ๋ก๊ทธ๋จ์ ํ๋ก์ธ์ค ๊ด๋ฆฌ์
ํด๋น ํ๋ก์ธ์ค๊ฐ ์ฃฝ์ด๋ ๋ค์ ์ด๋ ค์ฃผ๋ ์ญํ ๋ฑ..
- ์ค์น: [ํฐ๋ฏธ๋] npm i pm2 -g
- ํ๋ก์ธ์ค ์คํ: [ํฐ๋ฏธ๋] pm2 start app.js
- ํ๋ก์ธ์ค ์ค๋จ: [ํฐ๋ฏธ๋] pm2 stop app.js