min1925k@gmail.com

Add post, Show posts

......@@ -14,6 +14,8 @@ var menuRouter = require('./routes/menu')
var csvRouter = require('./routes/csv')
var postRouter = require('./routes/post')
var postaddRouter = require('./routes/postadd')
var showpostRouter = require('./routes/showpost')
var app = express();
var router = express.Router();
......@@ -54,6 +56,7 @@ app.use('/lpg',lpgRouter)
app.use('/signup',signupRouter); // sign up page route
app.use('/post',postRouter);
app.use('/postadd',postaddRouter);
app.use('/showpost',showpostRouter);
app.use('/', indexRouter); // main page route
......
......@@ -22,7 +22,6 @@ Schema.createSchema = function(mongoose) {
this._password = password;
this.salt = this.makeSalt();
this.hashed_password = this.encryptPassword(password);
console.log('virtual password 호출됨 : ' + this.hashed_password);
})
.get(function() { return this._password });
......
......@@ -38,7 +38,7 @@ router.post('/process', function(req, res) {
console.dir(docs);
// 조회 결과에서 사용자 이름 확인
res.render('login.html')
res.render('/')
} else { // 조회된 레코드가 없는 경우 실패 응답 전송
......
......@@ -78,6 +78,81 @@ 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;
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
});
post.savePost(function(err, result) {
if (err) {
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;
}
}
console.log("글 데이터 추가함.");
console.log('글 작성', '포스팅 글을 생성했습니다. : ' + post._id);
return res.redirect('/post');
});
});
} else {
res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
res.write('<h2>데이터베이스 연결 실패</h2>');
res.end();
}
});
module.exports = router;
// var addpost = function(req, res) {
// console.log('post 모듈 안에 있는 addpost 호출됨.');
......
var Entities = require('html-entities').AllHtmlEntities;
var express = require('express');
var router = express.Router();
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');
// 데이터베이스 객체가 초기화된 경우
if (database.db) {
// 1. 글 리스트
database.PostModel.load(paramId, 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) {
console.dir(results);
res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
// 뷰 템플레이트를 이용하여 렌더링한 후 전송
var context = {
title: '글 조회 ',
posts: results,
Entities: Entities
};
req.app.render('showpost.ejs', context, function(err, html) {
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;
}
res.end(html);
});
} else {
res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
res.write('<h2>글 조회 실패</h2>');
res.end();
}
});
} else {
res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
res.write('<h2>데이터베이스 연결 실패</h2>');
res.end();
}
})
module.exports = router;
\ No newline at end of file
......@@ -76,13 +76,14 @@
<td>작성일</td>
</tr>
<div class="ui very relaxed selection celled list">
<% var noStart = 4; for(var i=0; i < posts.length; i++){ var curTitle=posts[i]._doc.title;
<% 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; %>
<tr align="center">
<td>
<%= curNo %>
</td>
<td>
<div class="fourteen wide column" onclick="javascript:window.location='/showpost/<%=posts[i]._id %>'">
<%= curTitle %>
</td>
<td>admin</td>
......
......@@ -54,22 +54,48 @@
</div>
</nav>
<!-- Page Content-->
<div class="container">
<br>
<div class="ui raised segment">
<a class="ui blue ribbon label">게시판</a>
<div class="ui blue fluid card">
<div class="content">
<br><br>
<a class="ui button" href='/postadd'>글쓰기</a>
</div>
</div>
</div>
</div>
<div class="ui raised segment">
<a class="ui blue ribbon label">게시판</a>
<span id="board_title">글쓰기</span>
<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>
<div class="two fields">
<div class="field">
<label>제목</label>
<input type="text" name="title" placeholder="제목 입력">
</div>
<div class="field"></div>
</div>
<div class="field">
<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>
<br><br>
</div>
</div>
</div>
</main>
<!-- Footer-->
<footer class="bg-dark py-4 mt-auto">
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<title>Modern Business - Start Bootstrap Template</title>
<!-- Favicon-->
<link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
<!-- Bootstrap icons-->
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.5.0/font/bootstrap-icons.css" rel="stylesheet" />
<!-- Core theme CSS (includes Bootstrap)-->
<link href="css/styles.css" rel="stylesheet" />
<script src="http://code.jquery.com/jquery-2.1.4.js"></script>
<script>
$(document).ready(function() {
var output = $("#contentsOutput").text();
console.log('output : ' + output);
$("#contentsOutput").html(output);
});
</script>
</head>
<body class="d-flex flex-column h-100">
<main class="flex-shrink-0">
<!-- Navigation-->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container px-5">
<a class="navbar-brand" href="/">휴게소 정보</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation"><span
class="navbar-toggler-icon"></span></button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ms-auto mb-2 mb-lg-0">
<li class="nav-item"><a class="nav-link" href="/">Home</a></li>
<li class="nav-item"><a class="nav-link" href="/menu">휴게소 메뉴</a></li>
<li class="nav-item"><a class="nav-link" href="/weather">날씨</a></li>
<li class="nav-item"><a class="nav-link" href="/lpg">LPG</a></li>
<li class="nav-item"><a class="nav-link" href="/post">Board</a></li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarDropdownBlog" href="#" role="button"
data-bs-toggle="dropdown" aria-expanded="false">Blog</a>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdownBlog">
<li><a class="dropdown-item" href="blog-home.html">Blog Home</a></li>
<li><a class="dropdown-item" href="blog-post.html">Blog Post</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarDropdownLogin" href="#" role="button"
data-bs-toggle="dropdown" aria-expanded="false">Login</a>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdownLogin">
<li><a class="dropdown-item" href="/login">Login</a></li>
<li><a class="dropdown-item" href="/signup">Sign-up</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<!-- Page Content-->
<div class="ui raised segment">
<a class="ui blue ribbon label">게시판</a>
<span id="board_title"><%=title %></span>
<div class="ui blue fluid card">
<div class="content">
<%
var curTitle = posts._doc.title;
var curContents = posts._doc.contents;
var curWriter = "admin";
var entities = new Entities();
var decodedContents = entities.decode(curContents);
%>
<div id="titleOutput" class="header"><%=curTitle %></div>
<div class="meta">
<span class="right floated time">1일전</span>
<span class="category">일반</span>
</div>
<div id="contentsOutput" class="description">
<%=decodedContents %>
</div>
</div>
<div class="extra content">
<div class="right floated author">
<%=curWriter %>
</div>
</div>
</div>
<br><br><a class="ui button" href='/post'>글 목록</a>
</div>
</main>
<!-- Footer-->
<footer class="bg-dark py-4 mt-auto">
<div class="container px-5">
<div class="row align-items-center justify-content-between flex-column flex-sm-row">
<div class="col-auto">
<div class="small m-0 text-white">Copyright &copy; Your Website 2022</div>
</div>
<div class="col-auto">
<a class="link-light small" href="#!">Privacy</a>
<span class="text-white mx-1">&middot;</span>
<a class="link-light small" href="#!">Terms</a>
<span class="text-white mx-1">&middot;</span>
<a class="link-light small" href="#!">Contact</a>
</div>
</div>
</div>
</footer>
<!-- Bootstrap core JS-->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<!-- Core theme JS-->
<script src="js/scripts.js"></script>
</body>
</html>
\ No newline at end of file
......@@ -555,9 +555,9 @@
}
},
"node_modules/html-entities": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/html-entities/-/html-entities-2.3.3.tgz",
"integrity": "sha512-DV5Ln36z34NNTDgnz0EWGBLZENelNAtkiFA4kyNOG2tDI6Mz1uSWiq1wAKdyjnJwyDiDO7Fa2SO1CTxPXL8VxA=="
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/html-entities/-/html-entities-1.4.0.tgz",
"integrity": "sha512-8nxjcBcd8wovbeKx7h3wTji4e6+rhaVuPNpMqwWgnHh+N9ToqsCs6XztWRBPQ+UtzsoMAdKZtUENoVzU/EMtZA=="
},
"node_modules/http": {
"version": "0.0.1-security",
......
......@@ -17,7 +17,7 @@
"express": "^4.18.1",
"express-error-handler": "^1.1.0",
"express-session": "^1.17.3",
"html-entities": "^2.3.3",
"html-entities": "^1.2.0",
"http": "^0.0.1-security",
"mongoose": "^6.3.4",
"mysql": "^2.18.1",
......@@ -576,9 +576,9 @@
}
},
"node_modules/html-entities": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/html-entities/-/html-entities-2.3.3.tgz",
"integrity": "sha512-DV5Ln36z34NNTDgnz0EWGBLZENelNAtkiFA4kyNOG2tDI6Mz1uSWiq1wAKdyjnJwyDiDO7Fa2SO1CTxPXL8VxA=="
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/html-entities/-/html-entities-1.4.0.tgz",
"integrity": "sha512-8nxjcBcd8wovbeKx7h3wTji4e6+rhaVuPNpMqwWgnHh+N9ToqsCs6XztWRBPQ+UtzsoMAdKZtUENoVzU/EMtZA=="
},
"node_modules/http": {
"version": "0.0.1-security",
......@@ -1706,9 +1706,9 @@
"integrity": "sha512-l3LCuF6MgDNwTDKkdYGEihYjt5pRPbEg46rtlmnSPlUbgmB8LOIrKJbYYFBSbnPaJexMKtiPO8hmeRjRz2Td+A=="
},
"html-entities": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/html-entities/-/html-entities-2.3.3.tgz",
"integrity": "sha512-DV5Ln36z34NNTDgnz0EWGBLZENelNAtkiFA4kyNOG2tDI6Mz1uSWiq1wAKdyjnJwyDiDO7Fa2SO1CTxPXL8VxA=="
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/html-entities/-/html-entities-1.4.0.tgz",
"integrity": "sha512-8nxjcBcd8wovbeKx7h3wTji4e6+rhaVuPNpMqwWgnHh+N9ToqsCs6XztWRBPQ+UtzsoMAdKZtUENoVzU/EMtZA=="
},
"http": {
"version": "0.0.1-security",
......
......@@ -17,7 +17,7 @@
"express": "^4.18.1",
"express-error-handler": "^1.1.0",
"express-session": "^1.17.3",
"html-entities": "^2.3.3",
"html-entities": "^1.2.0",
"http": "^0.0.1-security",
"mongoose": "^6.3.4",
"mysql": "^2.18.1",
......