min1925k@gmail.com

Post writer, logout debug

...@@ -6,10 +6,10 @@ var SchemaObj = {}; ...@@ -6,10 +6,10 @@ var SchemaObj = {};
6 var PostSchema = mongoose.Schema({ 6 var PostSchema = mongoose.Schema({
7 title: {type: String, trim: true, 'default':''}, // 글 제목 7 title: {type: String, trim: true, 'default':''}, // 글 제목
8 contents: {type: String, trim:true, 'default':''}, // 글 내용 8 contents: {type: String, trim:true, 'default':''}, // 글 내용
9 - writer: {type: mongoose.Schema.ObjectId, ref: 'users'}, // 글쓴 사람 9 + writer: {type: String, 'default': ''}, // 글쓴 사람
10 comments: [{ // 댓글 10 comments: [{ // 댓글
11 contents: {type: String, trim:true, 'default': ''}, // 댓글 내용 11 contents: {type: String, trim:true, 'default': ''}, // 댓글 내용
12 - writer: {type: mongoose.Schema.ObjectId, ref: 'users'}, 12 + writer: {type: String, 'default': ''},
13 created_at: {type: Date, 'default': Date.now} 13 created_at: {type: Date, 'default': Date.now}
14 }], 14 }],
15 tags: {type: [], 'default': ''}, 15 tags: {type: [], 'default': ''},
......
...@@ -45,7 +45,9 @@ router.post('/process', function(req, res) { ...@@ -45,7 +45,9 @@ router.post('/process', function(req, res) {
45 45
46 46
47 } else { // 조회된 레코드가 없는 경우 실패 응답 전송 47 } else { // 조회된 레코드가 없는 경우 실패 응답 전송
48 - res.render('login.ejs'); 48 + res.render('login.ejs',{
49 + session : req.session
50 + });
49 51
50 } 52 }
51 }); 53 });
......
...@@ -7,8 +7,8 @@ var router = express.Router(); ...@@ -7,8 +7,8 @@ var router = express.Router();
7 router.get('/',function(req,res){ 7 router.get('/',function(req,res){
8 req.session.destroy(function() { 8 req.session.destroy(function() {
9 req.session; 9 req.session;
10 + res.redirect('/');
10 }); 11 });
11 - res.render('index.ejs')
12 }); 12 });
13 13
14 module.exports = router 14 module.exports = router
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -82,46 +82,19 @@ router.get('/',function(req,res){ ...@@ -82,46 +82,19 @@ router.get('/',function(req,res){
82 router.post('/addpost', function(req, res) { 82 router.post('/addpost', function(req, res) {
83 var paramTitle = req.body.title || req.query.title; 83 var paramTitle = req.body.title || req.query.title;
84 var paramContents = req.body.contents || req.query.contents; 84 var paramContents = req.body.contents || req.query.contents;
85 - var paramWriter = req.body.writer || req.query.writer; 85 + var paramWriter = req.session.sessId;
86 86
87 - console.log('요청 파라미터 : ' + paramTitle + ', ' + paramContents + ', ' +
88 - paramWriter);
89 -
90 var database = req.app.get('database'); 87 var database = req.app.get('database');
91 88
92 // 데이터베이스 객체가 초기화된 경우 89 // 데이터베이스 객체가 초기화된 경우
93 if (database.db) { 90 if (database.db) {
94 91
95 - // 1. 아이디를 이용해 사용자 검색
96 - database.UserModel.findById(paramWriter, function(err, results) {
97 - if (err) {
98 - console.error('게시판 글 추가 중 에러 발생 : ' + err.stack);
99 -
100 - res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
101 - res.write('<h2>게시판 글 추가 중 에러 발생</h2>');
102 - res.write('<p>' + err.stack + '</p>');
103 - res.end();
104 -
105 - return;
106 - }
107 -
108 - if (results == undefined || results.length < 1) {
109 - res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
110 - res.write('<h2>사용자 [' + paramWriter + ']를 찾을 수 없습니다.</h2>');
111 - res.end();
112 -
113 - return;
114 - }
115 -
116 - var userObjectId = results[0]._doc._id;
117 - console.log('사용자 ObjectId : ' + paramWriter +' -> ' + userObjectId);
118 -
119 // save()로 저장 92 // save()로 저장
120 // PostModel 인스턴스 생성 93 // PostModel 인스턴스 생성
121 var post = new database.PostModel({ 94 var post = new database.PostModel({
122 title: paramTitle, 95 title: paramTitle,
123 contents: paramContents, 96 contents: paramContents,
124 - writer: userObjectId 97 + writer: paramWriter
125 }); 98 });
126 99
127 post.savePost(function(err, result) { 100 post.savePost(function(err, result) {
...@@ -143,14 +116,7 @@ router.post('/addpost', function(req, res) { ...@@ -143,14 +116,7 @@ router.post('/addpost', function(req, res) {
143 116
144 return res.redirect('/post'); 117 return res.redirect('/post');
145 }); 118 });
146 - 119 + }
147 - });
148 -
149 - } else {
150 - res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
151 - res.write('<h2>데이터베이스 연결 실패</h2>');
152 - res.end();
153 - }
154 }); 120 });
155 121
156 module.exports = router; 122 module.exports = router;
......
...@@ -6,9 +6,7 @@ router.get('/:id',function(req,res){ ...@@ -6,9 +6,7 @@ router.get('/:id',function(req,res){
6 6
7 // URL 파라미터로 전달됨 7 // URL 파라미터로 전달됨
8 var paramId = req.body.id || req.query.id || req.params.id; 8 var paramId = req.body.id || req.query.id || req.params.id;
9 - 9 +
10 - console.log('요청 파라미터 : ' + paramId);
11 -
12 10
13 var database = req.app.get('database'); 11 var database = req.app.get('database');
14 12
...@@ -28,7 +26,6 @@ router.get('/:id',function(req,res){ ...@@ -28,7 +26,6 @@ router.get('/:id',function(req,res){
28 } 26 }
29 27
30 if (results) { 28 if (results) {
31 - console.dir(results);
32 29
33 res.writeHead('200', {'Content-Type':'text/html;charset=utf8'}); 30 res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
34 31
......
...@@ -4,9 +4,6 @@ ...@@ -4,9 +4,6 @@
4 <br> 4 <br>
5 5
6 <div class="ui raised segment"> 6 <div class="ui raised segment">
7 - <a class="ui blue ribbon label">게시판</a>
8 -
9 -
10 <div class="ui blue fluid card"> 7 <div class="ui blue fluid card">
11 <div class="content"> 8 <div class="content">
12 <table border="1" width="800" align="center"> 9 <table border="1" width="800" align="center">
...@@ -23,7 +20,7 @@ ...@@ -23,7 +20,7 @@
23 </tr> 20 </tr>
24 <div class="ui very relaxed selection celled list"> 21 <div class="ui very relaxed selection celled list">
25 <% var noStart = posts.length; for(var i=0; i < posts.length; i++){ var curTitle=posts[i]._doc.title; 22 <% var noStart = posts.length; for(var i=0; i < posts.length; i++){ var curTitle=posts[i]._doc.title;
26 - var curNo=noStart - i; var createdDate = posts[i]._doc.created_at; %> 23 + var curNo=noStart - i; var createdDate = posts[i]._doc.created_at.toLocaleString(); curWriter = posts[i]._doc.writer;%>
27 <tr align="center"> 24 <tr align="center">
28 <td> 25 <td>
29 <%= curNo %> 26 <%= curNo %>
...@@ -32,7 +29,9 @@ ...@@ -32,7 +29,9 @@
32 <div class="fourteen wide column" onclick="javascript:window.location='/showpost/<%=posts[i]._id %>'"> 29 <div class="fourteen wide column" onclick="javascript:window.location='/showpost/<%=posts[i]._id %>'">
33 <%= curTitle %> 30 <%= curTitle %>
34 </td> 31 </td>
35 - <td>admin</td> 32 + <td>
33 + <%= curWriter %>
34 + </td>
36 <td><%=createdDate%></td> 35 <td><%=createdDate%></td>
37 </tr> 36 </tr>
38 <% } %> 37 <% } %>
......
1 <%- include('./header.ejs') %> 1 <%- include('./header.ejs') %>
2 - 2 +<% if(session.sessId){ %>
3 - <!-- Page Content--> 3 + <!-- Page Content-->
4 - <div class="ui raised segment"> 4 + <div class="ui raised segment">
5 <a class="ui blue ribbon label">게시판</a> 5 <a class="ui blue ribbon label">게시판</a>
6 <span id="board_title">글쓰기</span> 6 <span id="board_title">글쓰기</span>
7 7
...@@ -24,15 +24,7 @@ ...@@ -24,15 +24,7 @@
24 <label>내용</label> 24 <label>내용</label>
25 <textarea id="contents" name="contents" rows="10" cols="80"></textarea> 25 <textarea id="contents" name="contents" rows="10" cols="80"></textarea>
26 </div> 26 </div>
27 - 27 +
28 - <div class="two fields">
29 - <div class="field">
30 - <label>작성자</label>
31 - <input type="text" name="writer" placeholder="작성자아이디 입력">
32 - </div>
33 - <div class="field"></div>
34 - </div>
35 -
36 <input class="ui submit button" type="submit" value="추가" name="" /> 28 <input class="ui submit button" type="submit" value="추가" name="" />
37 <input class="ui button" type="button" value="취소" name="" onclick="javascript:history.back()" /> 29 <input class="ui button" type="button" value="취소" name="" onclick="javascript:history.back()" />
38 </form> 30 </form>
...@@ -44,5 +36,21 @@ ...@@ -44,5 +36,21 @@
44 </div> 36 </div>
45 37
46 </main> 38 </main>
39 +<% } else { %>
40 + <!-- Page Content-->
41 + <div class="ui blue fluid card">
42 + <div class="content">
43 +
44 + <form class="content ui form" method="post" action="/post/addpost">
45 + <h4 class="ui dividing header">로그인이 필요합니다.</h4>
46 + <br><br>
47 + <a class="nav-link" href="/post">되돌아가기</a>
48 +
49 + </div>
50 + </div>
51 + </div>
52 +
53 + </main>
54 +<% } %>
47 <!-- Footer--> 55 <!-- Footer-->
48 <%- include('./footer.ejs') %> 56 <%- include('./footer.ejs') %>
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
11 <% 11 <%
12 var curTitle = posts._doc.title; 12 var curTitle = posts._doc.title;
13 var curContents = posts._doc.contents; 13 var curContents = posts._doc.contents;
14 - var curWriter = "admin"; 14 + var curWriter = posts._doc.writer;
15 15
16 var entities = new Entities(); 16 var entities = new Entities();
17 17
......