DB์ ์๋ ๋ฐ์ดํฐ ๊ฒ์ํ ํ์ด์ง๋ก ๋๊ธฐ๊ธฐ
์ด์ ํฌ์คํ ์์๋ DB๋ฅผ ์ ํ ํ์ต๋๋ค. ๊ทธ๋ผ DB์ ์๋ ๋ฐ์ดํฐ๋ฅผ ํ์ํด์ผํ๊ฒ ์ฃ ? ๊ทธ๋ผ server์ client ๊ฐ์ ํต์ ์ด ํ์ํ ๊ฒ์ ๋๋ค. ์ฝ๊ฒ ๋งํ๋ฉด ์ ๋จ๊ณผ ๋ท ๋จ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์์ผ ํ๋ค๋ ๊ฒ์ด์ฃ .
commuity ๋ผ๋ ํ์ด์ง๋ฅผ ๋ง๋ค์ด์ ์ ์ฉ์ ํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
์ ๋ pug ํ ํ๋ฆฟ์ ์ฌ์ฉํ๊ธฐ๋๋ฌธ์ html ํํ๊ฐ ์กฐ๊ธ ๋ค๋ฅผ ์ ์์ต๋๋ค. ๋ณํ์ ํด์ฃผ๋ ์ฌ์ดํธ๋ค์ด ๋ง์ด ์์ผ๋ ๋ฐ๊ฟ์ ์ฌ์ฉํ์ ๋ ๋๊ณ ์๋๋ฉด ๊ฐ์ธ์ ์ผ๋ก ๊ฒ์ํ ํผ์ ๋ง๋ค์ด์ ์ฐ์ ๋ ๋ฉ๋๋ค.
views/commuity.pug
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 | script(src='https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js') link#custom-css(href='custom_css/commuity.css', rel='stylesheet') link(rel='stylesheet', href='https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/css/bootstrap-select.min.css') table.type09(id='List') thead tr th(scope='cols' style="width:10%") nums th(scope='cols' style="width:55%") Title th(scope='cols' style="width:10%") Views th(scope='cols') Date th(scope='cols' style="width:10%") Author tbody#tbody div(align="right" style="margin-right:10%;") script. let data = !{data} let len = Object.keys(data).length $(document).ready(function(){ let tbody = document.getElementById('tbody'); if(data==0) len = 1 else{ len = Object.keys(data).length tbody = document.getElementById('tbody'); for(let i=0;i<len;i++){ tbody.innerHTML += "<th>"+(i+1)+"</th>" +"<th>"+data[i].subject+"</th>" +"<th>"+data[i].count+"</th>" +"<th>"+data[i].created_at.substr(0,10)+ "</th>" +"<th>"+data[i].name+"</th>" } } }); | cs |
์ฝ๊ฐ์ css๊ฐ ๋ค์ด๊ฐ์์ด์ ๊ฐ์ด ์ฒจ๋ถํ๊ฒ ์ต๋๋ค. ํ ์ด๋ธ์ ์ข ๋ณด๊ธฐ ์ข๊ฒ ํ๊ธฐ ์ํ ๊ฒ์ ๋๋ค.
custom_css/commuity.css
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 | table.type09 { margin-left: auto; margin-right: auto; border-collapse: collapse; line-height: 1.5; width: 80%; margin-top:3%; } table.type09 thead th { padding: 10px; font-weight: bold; vertical-align: top; text-align:left; color: #369; border-bottom: 3px solid #036; } table.type09 tbody th { width: 150px; padding: 10px; font-weight: bold; text-align:center; vertical-align: top; border-bottom: 1px solid #ccc; background: #f3f6f7; } table.type09 td { width: 350px; padding: 10px; vertical-align: top; border-bottom: 1px solid #ccc; } | cs |
๊ทธ๋ผ ํด๋น ํ์ด์ง๋ฅผ app.js ํ์ผ์์ ๋ ๋๋ง์ ํ๊ฒ ๋๋ฉด html ํ์ด์ง๊ฐ ๋์ฌ ๊ฒ์ ๋๋ค.
app.js
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | const createError = require('http-errors'); const express = require('express'); const path = require('path'); const cookieParser = require('cookie-parser'); const bodyParser = require('body-parser'); const session = require('express-session') const passport = require('passport') const sequelize = require('sequelize') const models = require('./models') const app = express(); //app.get const indexRouter = require('./routes/index'); const commuityRouter = require('./routes/commuity') //sequelize models.sequelize.sync() .then(function() { console.log('โ DB connection success.'); console.log(' Press CTRL-C to stop\n'); }) .catch(function(err) { console.error(err); console.log('โ DB connection error. Please make sure DB is running.'); process.exit(); }); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'pug'); app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use(bodyParser.urlencoded({extended: false})); app.use(bodyParser.json()); app.use(cookieParser()) app.use(session({ secret:'123123123', resave:false, saveUninitialize:true })); app.use(express.static(__dirname)); //get app.use('/', indexRouter); app.use('/commuity',commuityRouter); // catch 404 and forward to error handler app.use(function(req, res, next) { next(createError(404)); }); // error handler app.use(function(err, req, res, next) { // set locals, only providing error in development res.locals.message = err.message; res.locals.error = req.app.get('env') === 'development' ? err : {}; // render the error page res.status(err.status || 500); res.render('error'); }); module.exports = app; | cs |
์ฌ๊ธฐ์ commuity ๋ผ์ฐํฐ๋ฅผ ๋ถ๋ฌ์ค๋ฉด
routes/commuity.js
1 2 3 4 5 6 7 8 9 10 | let express = require('express') let router = express.Router() let app = express(); let path = require('path') app.set('views',path.join(__dirname,'views')); app.set('view engine','pug') router.get('/',function(req,res){ res.render('commuity') }) | cs |
์์ง์ ๋ฐ์ดํฐ๋ฅผ ๋๊ฒจ์ค ๊ฒ์ด ํ๋๋ ์์ผ๋ ์ด๋ฐ ์์ผ๋ก ์ด๋ง ๋์ค๊ฒ ๋ ๊ฒ์
๋๋ค. commuity ๋ผ์ฐํฐ์์ ๋ฐ์ดํฐ๋ฅผ ๋๊ฒจ์ฃผ๋ ์์
์ ํด์ผํฉ๋๋ค. ๊ทธ๋ ๋ค๊ณ ์๋ฌด ๋ฐ์ดํฐ๋ ๋๊ฒจ์ค ์ ์๊ฒ ์ฃ DB์ ์ฐ๋ํด์ ์ฐ๊ณ ์์ผ๋ DB์ ์๋ ๋ฐ์ดํฐ๋ฅผ ๋๊ฒจ์ฃผ๊ฒ ์ต๋๋ค.
์ ๋ DB์ ์๋ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ์์ ์ ํจ์๋ก ๋ฐ๋ก ์์ฑํ์ฌ ๋ถ๋ฌ์๋๋ฐ ๊ทธ๋ฅ ํ ํ์ผ์ function์ ์ ์ธํด์ ์ฐ์ ๋ ์๊ด์ ์์ต๋๋ค.
function/board.js
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 42 43 44 45 46 47 48 49 50 51 52 53 | let models = require('../models'); let async = require('async'); function boardLoadFunction(callback) { models.Board.findAll().then(function(result){ //result๊ฐ board ํ
์ด๋ธ์ ๋ชจ๋ ๊ฐ์ json์ผ๋ก ๋ฐํ let jsonObj = []; //board ํ
์ด๋ธ์ ๋ฐ์ดํฐ๋ฅผ ๋ด์ ๋ฐฐ์ด let tasks = [ function(call){ if(result.length==0) //๊ฒ์ํ์ ๊ธ์ด ํ๋๋ ์์ ๋0๊ฐ ๋ฆฌํด callback(0) else{ for(let i=0;i<result.length;i++) //๋ฐ์ดํฐ์ ๊ฐ์๋งํผ ๋ฐ๋ณต๋ฌธ jsonObj.push(result[i].dataValues) //jsonObj ๋ฐฐ์ด์ select * from board ๊ฐ์ ์ ์ฅ call(null,jsonObj); //์์ ์ฟผ๋ฆฌ๋ฌธ ๊ฒฐ๊ณผ๋ฅผ ๋ค์ ํจ์๋ก ๋๊ฒจ์ค } }, function(jsonObj,call){ let i =0; let nameList = {}; //{ ์ธ๋ํค : ์ด๋ฆ } ์ ํํ๋ก ๋ง๋ค json ๋ฐ์ดํฐ jsonObj.forEach(function(element){ //board ํ
์ด๋ธ๊ณผ userํ
์ด๋ธ์ ๋ฐ์ดํฐ๋ฅผ ๋น๊ต models.User.findOne({ where:{id:element.fk_userId} //board์ ์ธ๋ํค์ user์ id๊ฐ์ ์ผ์น์ฌ๋ถ }).then(function(user){ nameList[element.fk_userId] = user.dataValues.name //nameList ๊ฐ์ฒด์ { ์ธ๋ํค : ์ด๋ฆ } ์ผ๋ก ์ ์ฅ i++; if(jsonObj.length==i) //๋ฐฐ์ด ๋๊น์ง ๋น๊ต๊ฐ ๋๋๋ฉด ๋ค์ ํจ์๋ก ๋๊ฒจ์ค call(null,nameList); }) }) }, function(nameList,call) { //jsonObj์ name๊ฐ์ ์ถ๊ฐํ ๋ฐ์ดํฐ๋ฅผ ์ต์ข
์ ์ผ๋ก ๋ง๋ฆ let i=0 jsonObj.forEach(function(element){ if(nameList.hasOwnProperty(element.fk_userId)){ jsonObj[i].name = nameList[element.fk_userId] //jsonObj๊ฐ์ฒด์ name key์ key value ์ถ๊ฐ i++ } if(i==jsonObj.length) callback(jsonObj) //๋ฐฐ์ด ๋๊น์ง ๋น๊ต๊ฐ ๋๋๋ฉด app.js๋ก ๋๊ฒจ์ค }) } ] async.waterfall(tasks,function(err){ if(err) console.log(err) }) }).catch(function(err){ console.log(err); }) } exports.boardLoadFunction = boardLoadFunction; | cs |
์์ฑํ๋ค๋ณด๋ ์ฌ์ด์ฆ๊ฐ ๋๋ฌด ์ปค์ ธ๋ฒ๋ ธ์ต๋๋ค. ์ ๊ฐ ๊ฒ์ํ์ ๋ํ๋๊ฒ ํ ์ด์ ๋ชฉ๋ก์ ์์์ ๋ณด๋ค์ํผ
๊ฒ์๋ฌผ ๋ฒํธ | ๊ฒ์๋ฌผ ์ ๋ชฉ | ์กฐํ ์ | ๋ ์ง | ๊ธ์ด์ด |
์ด๋ ๊ฒ 6๊ฐ์ ์ด์ธ๋ฐ ๊ฒ์๋ฌผ ๋ฒํธ๋ฅผ ์ ์ธํ๊ณ ๋ board ํ ์ด๋ธ๊ณผ user ํ ์ด๋ธ์ ํด๋น ์ ๋ณด๊ฐ ์์ต๋๋ค. ์ฆ, ๋ ํ ์ด๋ธ์ ๋ชจ๋ ์ฐธ์กฐํด์ผ ํ๋ค๋ ๋ป์ด ๋์ฃ ..
board ํ ์ด๋ธ -> ๊ฒ์๋ฌผ์ ๋ชฉ , ์กฐํ์ , ๋ ์ง
user ํ ์ด๋ธ -> ๊ธ์ด์ด
๊ทธ๋์ ์์ ํจ์ ์์ ์ ๋จ๊ณ๋ณ๋ก ๋๋์๋ฉด ์ด๋ ์ต๋๋ค.
๊ทผ๋ฐ async ๋ชจ๋์ ์จ๊ฐ๋ฉด์๊น์ง ํ ์ด์ ๋ user ํ ์ด๋ธ์์ name ๊ฐ์ ๊ฒ์ํ์ฌ ๊ธฐ์กด์ jsonObj ๋ณ์์ ์ ์ฅ์ ํ ๋ ๋งค์นญ์ด ์๋๋ ๋ฌธ์ ๊ฐ ์๊ฒผ์ต๋๋ค. ์๋ฅผ๋ค๋ฉด 1๋ฒ์ด๋ผ๋ ๊ฒ์๋ฌผ์ 1๋ฒ์ด๋ผ๋ ์ด๋ฆ์ด ์์ฑํ๋ค๊ณ ๊ฐ์ ํ์ ๋
๊ฒ์๋ฌผ ๋ฒํธ | ๊ฒ์๋ฌผ ์ ๋ชฉ | ์กฐํ ์ | ๋ ์ง | ๊ธ์ด์ด |
1 1๋ฒ 0 ~~~ 1๋ฒ
2 2๋ฒ 0 ~~~ 2๋ฒ
์ด๋ฐ ์์ผ๋ก ๋์์ผ ํ๋๋ฐ
๊ฒ์๋ฌผ ๋ฒํธ | ๊ฒ์๋ฌผ ์ ๋ชฉ | ์กฐํ ์ | ๋ ์ง | ๊ธ์ด์ด |
1 1๋ฒ 0 ~~~ 2๋ฒ
2 2๋ฒ 0 ~~~ 1๋ฒ
์ด๋ฐ์์ผ๋ก ๊ธ์ด์ด์ ๊ฒ์๋ฌผ์ด ๋งค์นญ์ด ์๋๋ ๋ฌธ์ ๊ฐ ์๊ธฐ๊ฒ ๋์ด ์ต๋ํ ๋จ๊ณ๋ณ๋ก ํ๋์ ๋ฐ์ดํฐ๋ง ์ฝ์ ํ๊ฒ ๋ ์์ ์ ํ๊ฒ ๋์์ต๋๋ค. ์๋ง ๋น๋๊ธฐ์ฑ๋๋ฌธ์ ๋ฐ์ดํฐ ์์๊ฐ ๋ฌ๋ผ์ง๋ ๋ฏ ํฉ๋๋ค.
๊ฐ๋
์ฑ๋ ๋จ์ด์ง๊ณ ๋นํจ์จ์ ์ธ ๋ฐฉ๋ฒ์ด๋ผ ์๊ฐ์ ๋๋๋ฐ ๋ฐฉ๋ฒ์ ์ฐพ์๋ด๋ฉด ์
๋ฐ์ดํธ ํ๋๋ก ํ๊ฒ ์ต๋๋ค.
์ ํจ์์ ๊ฒฐ๊ณผ๋ฌผ์ ๋งค๊ฐ๋ณ์๋ก ์ฝ๋ฐฑํจ์๋ฅผ ๋ฐ์์ ์ต์ข ์ ์ผ๋ก ๋ง๋ค์ด์ง jsonObj๊ฐ์ boardLoadFunction์ ํธ์ถํ ๊ณณ์์ ์ฌ์ฉํ ์ ์๊ฒ๋ ํด์ค๋๋ค. ๊ฒฐ๊ณผ๋ฌผ๋ก ๋ง๋ค์ด์ง ๊ฐ์ฒด๋ html ํ์ด์ง๋ฅผ ๋ก๋ฉํจ๊ณผ ๋์์ ๋ฐ์ดํฐ๋ฅผ ๋๊ฒจ์ค์ผ๊ฒ ์ฃ
์์ ๋
routes/commuity.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | let express = require('express') let router = express.Router() let app = express(); let path = require('path') let board = require('../function/board') app.set('views',path.join(__dirname,'views')); app.set('view engine','pug') router.get('/',function(req,res){ board.boardLoadFunction(function(result){ res.render('commuity',{data:JSON.stringify(result)}) }) }) module.exports = router; | cs |
board ํจ์๋ฅผ ์ถ๊ฐํ์ฌ ํด๋น ๊ฐ์ commuity ํ์ด์ง๋ฅผ ๋ ๋๋งํ๋ฉด์ ๊ฐ์ฒด๋ฅผ client ํ์ด์ง์ ๋๊ฒจ์ฃผ๊ฒ ๋ฉ๋๋ค.
๋งจ ์ฒ์์ ์์ฑํ๋ html ํ์ด์ง ์ค script ๋ถ๋ถ๋ง ๋ฐ๋ก ๋ผ์ด์ ๋ณด๊ฒ ์ต๋๋ค.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <script> let data = !{data} let len = Object.keys(data).length $(document).ready(function() { let tbody = document.getElementById('tbody'); if (data == 0) len = 1 else { len = Object.keys(data).length tbody = document.getElementById('tbody'); for (let i = 0; i < len; i++) { tbody.innerHTML += "<th>" + (i + 1) + "</th>" + "<th>" + data[i].subject + "</th>" + "<th>" + data[i].count + "</th>" + "<th>" + data[i].created_at.substr(0, 10) + "</th>" + "<th>" + data[i].name + "</th>" } } }); </script> | cs |
data ๋ณ์๋ board ํจ์์์ ๋ง๋ค์ด์ง ๊ฒ์๋ฌผ์ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ json ๊ฐ์ฒด ๋ฐ์ดํฐ ์ ๋๋ค.
ํด๋น ๋ฐ์ดํฐ๋ฅผ ๋ฐ๋ณต๋ฌธ์ ํตํด์ ํ ์ด๋ธ์ ์ฐจ๋ก๋๋ก ์ฝ์ ์์ผ์ฃผ๋ฉด ๋ฉ๋๋ค. innerHTML ๋ฅผ ์ด์ฉํ์ฌ data ๊ฐ์ฒด์ ์๋ ๊ฐ๋ค์ ํด๋น ์ด์ ๋ง๊ฒ๋ ๋ฐ์ดํฐ ํ์ ์ถ๊ฐํด์ฃผ๊ฒ ๋ฉ๋๋ค.
DB์ ๋ฐ์ดํฐ๊ฐ ์๋ค๊ณ ํ๋ค๋ฉด ์ด๋ฐ์์ผ๋ก ๋ชจ์ต์ด ๋์ค๊ฒ ๋๊ฒ ์ฃ
๋ฌผ๋ก ํด๋น ๊ฒ์๋ฌผ์ ๋ด์ฉ์ ๋ณผ ์๋ ์์ด์ผ ํ๊ณ ๊ธ์ฐ๊ธฐ ํ์ด์ง ๋ผ๋๊ฐ ๊ฒ์๊ธฐ๋ฅ๊ณผ ๊ฐ์ ๊ธฐ๋ฅ์ด ์์ด์ผ ๊ฒ์ํ์ด๋ผ ํ ์ ์๊ฒ ์ฃ .
์ง๊ธ์ ์ด๋ค์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ ๋ฟ๋ ค์ฃผ๋ ๊ฒ์ธ์ง๋ฅผ ์์ฃผ๋ก ๋ณด์์ต๋๋ค.