index.js
4.67 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const { WSAECONNRESET } = require('constants');
const e = require('express');
var express = require('express');
const { connect } = require('http2');
var router = express.Router();
var mysql = require('mysql');
var path = require('path') // 상대경로
var mysql_odbc = require('../../db/db_board')();
var board = mysql_odbc.init();
router.get('/list/:page', function(req, res, next) {
var id = req.user;
if(!id) res.redirect('/board/list/1')
else{
var page = req.params.page;
var sql = "select idx, name, title, date_format(modidate,'%Y-%m-%d %H:%i:%s') modidate, " +
"date_format(regdate,'%Y-%m-%d %H:%i:%s') regdate,hit from board";
board.query(sql, function(err,rows) {
if (err) console.error("err : " + err);
var id = req.user.ID;
var nickname = req.user.nickname;
console.log(req.user.ID+'('+nickname+') 유저가 게시판을 보고있습니다.')
res.render('list.ejs', {'ID':id, 'nickname': nickname, title: '게시판 리스트', rows: rows, page:page, length:rows.length-1,page_num:10,pass:true})
console.log(rows.length-1)
})
}
});
router.get('/list', function(req,res,next){
var id = req.user;
if(!id) res.sendFile(path.join(__dirname, "../../public/login.html"))
else res.redirect('/board/list/1')
})
router.get('/write', function(req,res,next){
var id = req.user;
if(!id) res.sendFile(path.join(__dirname, "../../public/login.html"))
else{
var id = req.user.ID;
var nickname = req.user.nickname;
console.log(req.user.ID+'('+nickname+') 유저가 게시글 작성 중입니다.')
res.render('write.ejs', {'ID':id, 'nickname': nickname, title:"게시판 글 쓰기"})
}
})
router.post('/write', function(req,res,next){
var name = req.body.name
var title = req.body.title
var content = req.body.content
var passwd = req.body.passwd
var datas = [name,title,content,passwd]
var sql = "insert into board(name, title, content, regdate, modidate, passwd,hit) values(?,?,?,now(),now(),?,0)";
board.query(sql,datas, function (err, rows) {
if (err) console.error("err : " + err);
var id = req.user.ID;
var nickname = req.user.nickname;
console.log(req.user.ID+'('+nickname+') 유저가 게시글을 작성했습니다.')
res.redirect('/board/list/1');
});
})
router.get('/read/:idx', function(req,res,next){
var idx = req.params.idx
var sql = "select idx, name, title, content, date_format(modidate,'%Y-%m-%d %H:%i:%s') modidate, " +
"date_format(regdate,'%Y-%m-%d %H:%i:%s') regdate,hit from board where idx=?";
board.query(sql,[idx], function(err,row){
if(err) console.error(err)
var id = req.user;
if(!id) res.redirect('/board/list')
else{
var id = req.user.ID;
var nickname = req.user.nickname;
console.log(req.user.ID+'('+nickname+') 유저가 '+idx+'번 게시글을 보고 있습니다.')
res.render('read.ejs', {'ID':id, 'nickname': nickname, title:"글 상세", row:row[0]})
}
})
})
router.post('/update', function(req,res,next){
var idx = req.body.idx
var name = req.body.name
var title = req.body.title
var content = req.body.content
var passwd = req.body.passwd
var datas = [name, title, content, idx, passwd]
var sql = "update board set name = ?,title=?,content=?,modidate=now() where idx =? and passwd=?"
board.query(sql,datas,function(err,result){
if(err) console.error(err)
if(result.affectedRows==0){
res.send("<script>alert('패스워드가 일치하지 않습니다.');history.back();</script>")
}
else{
var id = req.user.ID;
var nickname = req.user.nickname;
console.log(req.user.ID+'('+nickname+') 유저가 '+idx+'번 게시글을 수정했습니다.')
res.redirect('/board/read/'+idx)
}
})
})
router.post('/delete', function(req,res,next){
var idx = req.body.idx
var passwd = req.body.passwd
var datas = [idx,passwd]
var sql = "delete from board where idx=? and passwd=?"
board.query(sql,datas, function(err,result){
if(err) console.error(err)
if(result.affectedRows == 0){
res.send("<script>alert('패스워드가 일치하지 않습니다.');history.back();</script>");
}
else
{
var id = req.user.ID;
var nickname = req.user.nickname;
console.log(req.user.ID+'('+nickname+') 유저가 '+idx+'번 게시글을 삭제했습니다.')
res.redirect('/board/list/');
}
})
})
module.exports = router;