min1925k@gmail.com

Post writer, logout debug

......@@ -6,10 +6,10 @@ var SchemaObj = {};
var PostSchema = mongoose.Schema({
title: {type: String, trim: true, 'default':''}, // 글 제목
contents: {type: String, trim:true, 'default':''}, // 글 내용
writer: {type: mongoose.Schema.ObjectId, ref: 'users'}, // 글쓴 사람
writer: {type: String, 'default': ''}, // 글쓴 사람
comments: [{ // 댓글
contents: {type: String, trim:true, 'default': ''}, // 댓글 내용
writer: {type: mongoose.Schema.ObjectId, ref: 'users'},
writer: {type: String, 'default': ''},
created_at: {type: Date, 'default': Date.now}
}],
tags: {type: [], 'default': ''},
......
......@@ -45,7 +45,9 @@ router.post('/process', function(req, res) {
} else { // 조회된 레코드가 없는 경우 실패 응답 전송
res.render('login.ejs');
res.render('login.ejs',{
session : req.session
});
}
});
......
......@@ -7,8 +7,8 @@ var router = express.Router();
router.get('/',function(req,res){
req.session.destroy(function() {
req.session;
res.redirect('/');
});
res.render('index.ejs')
});
module.exports = router
\ No newline at end of file
......
......@@ -82,46 +82,19 @@ router.get('/',function(req,res){
router.post('/addpost', function(req, res) {
var paramTitle = req.body.title || req.query.title;
var paramContents = req.body.contents || req.query.contents;
var paramWriter = req.body.writer || req.query.writer;
var paramWriter = req.session.sessId;
console.log('요청 파라미터 : ' + paramTitle + ', ' + paramContents + ', ' +
paramWriter);
var database = req.app.get('database');
// 데이터베이스 객체가 초기화된 경우
if (database.db) {
// 1. 아이디를 이용해 사용자 검색
database.UserModel.findById(paramWriter, function(err, results) {
if (err) {
console.error('게시판 글 추가 중 에러 발생 : ' + err.stack);
res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
res.write('<h2>게시판 글 추가 중 에러 발생</h2>');
res.write('<p>' + err.stack + '</p>');
res.end();
return;
}
if (results == undefined || results.length < 1) {
res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
res.write('<h2>사용자 [' + paramWriter + ']를 찾을 수 없습니다.</h2>');
res.end();
return;
}
var userObjectId = results[0]._doc._id;
console.log('사용자 ObjectId : ' + paramWriter +' -> ' + userObjectId);
// save()로 저장
// PostModel 인스턴스 생성
var post = new database.PostModel({
title: paramTitle,
contents: paramContents,
writer: userObjectId
writer: paramWriter
});
post.savePost(function(err, result) {
......@@ -143,14 +116,7 @@ router.post('/addpost', function(req, res) {
return res.redirect('/post');
});
});
} else {
res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
res.write('<h2>데이터베이스 연결 실패</h2>');
res.end();
}
}
});
module.exports = router;
......
......@@ -6,9 +6,7 @@ router.get('/:id',function(req,res){
// URL 파라미터로 전달됨
var paramId = req.body.id || req.query.id || req.params.id;
console.log('요청 파라미터 : ' + paramId);
var database = req.app.get('database');
......@@ -28,7 +26,6 @@ router.get('/:id',function(req,res){
}
if (results) {
console.dir(results);
res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
......
......@@ -4,9 +4,6 @@
<br>
<div class="ui raised segment">
<a class="ui blue ribbon label">게시판</a>
<div class="ui blue fluid card">
<div class="content">
<table border="1" width="800" align="center">
......@@ -23,7 +20,7 @@
</tr>
<div class="ui very relaxed selection celled list">
<% var noStart = posts.length; for(var i=0; i < posts.length; i++){ var curTitle=posts[i]._doc.title;
var curNo=noStart - i; var createdDate = posts[i]._doc.created_at; %>
var curNo=noStart - i; var createdDate = posts[i]._doc.created_at.toLocaleString(); curWriter = posts[i]._doc.writer;%>
<tr align="center">
<td>
<%= curNo %>
......@@ -32,7 +29,9 @@
<div class="fourteen wide column" onclick="javascript:window.location='/showpost/<%=posts[i]._id %>'">
<%= curTitle %>
</td>
<td>admin</td>
<td>
<%= curWriter %>
</td>
<td><%=createdDate%></td>
</tr>
<% } %>
......
<%- include('./header.ejs') %>
<!-- Page Content-->
<div class="ui raised segment">
<% if(session.sessId){ %>
<!-- Page Content-->
<div class="ui raised segment">
<a class="ui blue ribbon label">게시판</a>
<span id="board_title">글쓰기</span>
......@@ -24,15 +24,7 @@
<label>내용</label>
<textarea id="contents" name="contents" rows="10" cols="80"></textarea>
</div>
<div class="two fields">
<div class="field">
<label>작성자</label>
<input type="text" name="writer" placeholder="작성자아이디 입력">
</div>
<div class="field"></div>
</div>
<input class="ui submit button" type="submit" value="추가" name="" />
<input class="ui button" type="button" value="취소" name="" onclick="javascript:history.back()" />
</form>
......@@ -44,5 +36,21 @@
</div>
</main>
<% } else { %>
<!-- Page Content-->
<div class="ui blue fluid card">
<div class="content">
<form class="content ui form" method="post" action="/post/addpost">
<h4 class="ui dividing header">로그인이 필요합니다.</h4>
<br><br>
<a class="nav-link" href="/post">되돌아가기</a>
</div>
</div>
</div>
</main>
<% } %>
<!-- Footer-->
<%- include('./footer.ejs') %>
......
......@@ -11,7 +11,7 @@
<%
var curTitle = posts._doc.title;
var curContents = posts._doc.contents;
var curWriter = "admin";
var curWriter = posts._doc.writer;
var entities = new Entities();
......