index.js
3.2 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
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 from board";
board.query(sql, function(err,rows) {
if (err) console.error("err : " + err);
var id = req.user.ID;
var nickname = req.user.nickname;
res.render('list.ejs', {'ID':id, 'nickname': nickname, title: '게시판 리스트', rows: rows})
})
}
});
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;
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);
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;
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
{
res.redirect('/board/read/'+idx)
}
})
})
module.exports = router;