min1925k@gmail.com

Add post, Show posts

...@@ -14,6 +14,8 @@ var menuRouter = require('./routes/menu') ...@@ -14,6 +14,8 @@ var menuRouter = require('./routes/menu')
14 var csvRouter = require('./routes/csv') 14 var csvRouter = require('./routes/csv')
15 var postRouter = require('./routes/post') 15 var postRouter = require('./routes/post')
16 var postaddRouter = require('./routes/postadd') 16 var postaddRouter = require('./routes/postadd')
17 +var showpostRouter = require('./routes/showpost')
18 +
17 19
18 var app = express(); 20 var app = express();
19 var router = express.Router(); 21 var router = express.Router();
...@@ -54,6 +56,7 @@ app.use('/lpg',lpgRouter) ...@@ -54,6 +56,7 @@ app.use('/lpg',lpgRouter)
54 app.use('/signup',signupRouter); // sign up page route 56 app.use('/signup',signupRouter); // sign up page route
55 app.use('/post',postRouter); 57 app.use('/post',postRouter);
56 app.use('/postadd',postaddRouter); 58 app.use('/postadd',postaddRouter);
59 +app.use('/showpost',showpostRouter);
57 app.use('/', indexRouter); // main page route 60 app.use('/', indexRouter); // main page route
58 61
59 62
......
...@@ -22,7 +22,6 @@ Schema.createSchema = function(mongoose) { ...@@ -22,7 +22,6 @@ Schema.createSchema = function(mongoose) {
22 this._password = password; 22 this._password = password;
23 this.salt = this.makeSalt(); 23 this.salt = this.makeSalt();
24 this.hashed_password = this.encryptPassword(password); 24 this.hashed_password = this.encryptPassword(password);
25 - console.log('virtual password 호출됨 : ' + this.hashed_password);
26 }) 25 })
27 .get(function() { return this._password }); 26 .get(function() { return this._password });
28 27
......
...@@ -38,7 +38,7 @@ router.post('/process', function(req, res) { ...@@ -38,7 +38,7 @@ router.post('/process', function(req, res) {
38 console.dir(docs); 38 console.dir(docs);
39 39
40 // 조회 결과에서 사용자 이름 확인 40 // 조회 결과에서 사용자 이름 확인
41 - res.render('login.html') 41 + res.render('/')
42 42
43 43
44 } else { // 조회된 레코드가 없는 경우 실패 응답 전송 44 } else { // 조회된 레코드가 없는 경우 실패 응답 전송
......
...@@ -78,6 +78,81 @@ router.get('/',function(req,res){ ...@@ -78,6 +78,81 @@ router.get('/',function(req,res){
78 78
79 }) 79 })
80 80
81 +
82 +router.post('/addpost', function(req, res) {
83 + var paramTitle = req.body.title || req.query.title;
84 + var paramContents = req.body.contents || req.query.contents;
85 + var paramWriter = req.body.writer || req.query.writer;
86 +
87 + console.log('요청 파라미터 : ' + paramTitle + ', ' + paramContents + ', ' +
88 + paramWriter);
89 +
90 + var database = req.app.get('database');
91 +
92 + // 데이터베이스 객체가 초기화된 경우
93 + if (database.db) {
94 +
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()로 저장
120 + // PostModel 인스턴스 생성
121 + var post = new database.PostModel({
122 + title: paramTitle,
123 + contents: paramContents,
124 + writer: userObjectId
125 + });
126 +
127 + post.savePost(function(err, result) {
128 + if (err) {
129 + if (err) {
130 + console.error('응답 웹문서 생성 중 에러 발생 : ' + err.stack);
131 +
132 + res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
133 + res.write('<h2>응답 웹문서 생성 중 에러 발생</h2>');
134 + res.write('<p>' + err.stack + '</p>');
135 + res.end();
136 +
137 + return;
138 + }
139 + }
140 +
141 + console.log("글 데이터 추가함.");
142 + console.log('글 작성', '포스팅 글을 생성했습니다. : ' + post._id);
143 +
144 + return res.redirect('/post');
145 + });
146 +
147 + });
148 +
149 + } else {
150 + res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
151 + res.write('<h2>데이터베이스 연결 실패</h2>');
152 + res.end();
153 + }
154 +});
155 +
81 module.exports = router; 156 module.exports = router;
82 // var addpost = function(req, res) { 157 // var addpost = function(req, res) {
83 // console.log('post 모듈 안에 있는 addpost 호출됨.'); 158 // console.log('post 모듈 안에 있는 addpost 호출됨.');
......
1 +var Entities = require('html-entities').AllHtmlEntities;
2 +var express = require('express');
3 +var router = express.Router();
4 +
5 +router.get('/:id',function(req,res){
6 +
7 + // URL 파라미터로 전달됨
8 + var paramId = req.body.id || req.query.id || req.params.id;
9 +
10 + console.log('요청 파라미터 : ' + paramId);
11 +
12 +
13 + var database = req.app.get('database');
14 +
15 + // 데이터베이스 객체가 초기화된 경우
16 + if (database.db) {
17 + // 1. 글 리스트
18 + database.PostModel.load(paramId, function(err, results) {
19 + if (err) {
20 + console.error('게시판 글 조회 중 에러 발생 : ' + err.stack);
21 +
22 + res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
23 + res.write('<h2>게시판 글 조회 중 에러 발생</h2>');
24 + res.write('<p>' + err.stack + '</p>');
25 + res.end();
26 +
27 + return;
28 + }
29 +
30 + if (results) {
31 + console.dir(results);
32 +
33 + res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
34 +
35 + // 뷰 템플레이트를 이용하여 렌더링한 후 전송
36 + var context = {
37 + title: '글 조회 ',
38 + posts: results,
39 + Entities: Entities
40 + };
41 +
42 + req.app.render('showpost.ejs', context, function(err, html) {
43 + if (err) {
44 + console.error('응답 웹문서 생성 중 에러 발생 : ' + err.stack);
45 +
46 + res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
47 + res.write('<h2>응답 웹문서 생성 중 에러 발생</h2>');
48 + res.write('<p>' + err.stack + '</p>');
49 + res.end();
50 +
51 + return;
52 + }
53 +
54 + res.end(html);
55 + });
56 +
57 + } else {
58 + res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
59 + res.write('<h2>글 조회 실패</h2>');
60 + res.end();
61 + }
62 + });
63 + } else {
64 + res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
65 + res.write('<h2>데이터베이스 연결 실패</h2>');
66 + res.end();
67 + }
68 +})
69 +
70 +module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
...@@ -76,13 +76,14 @@ ...@@ -76,13 +76,14 @@
76 <td>작성일</td> 76 <td>작성일</td>
77 </tr> 77 </tr>
78 <div class="ui very relaxed selection celled list"> 78 <div class="ui very relaxed selection celled list">
79 - <% var noStart = 4; for(var i=0; i < posts.length; i++){ var curTitle=posts[i]._doc.title; 79 + <% var noStart = posts.length; for(var i=0; i < posts.length; i++){ var curTitle=posts[i]._doc.title;
80 var curNo=noStart - i; var createdDate = posts[i]._doc.created_at; %> 80 var curNo=noStart - i; var createdDate = posts[i]._doc.created_at; %>
81 <tr align="center"> 81 <tr align="center">
82 <td> 82 <td>
83 <%= curNo %> 83 <%= curNo %>
84 </td> 84 </td>
85 <td> 85 <td>
86 + <div class="fourteen wide column" onclick="javascript:window.location='/showpost/<%=posts[i]._id %>'">
86 <%= curTitle %> 87 <%= curTitle %>
87 </td> 88 </td>
88 <td>admin</td> 89 <td>admin</td>
......
...@@ -54,22 +54,48 @@ ...@@ -54,22 +54,48 @@
54 </div> 54 </div>
55 </nav> 55 </nav>
56 <!-- Page Content--> 56 <!-- Page Content-->
57 - <div class="container"> 57 + <div class="ui raised segment">
58 - <br> 58 + <a class="ui blue ribbon label">게시판</a>
59 - 59 + <span id="board_title">글쓰기</span>
60 - <div class="ui raised segment"> 60 +
61 - <a class="ui blue ribbon label">게시판</a> 61 +
62 - 62 + <div class="ui blue fluid card">
63 - 63 + <div class="content">
64 - <div class="ui blue fluid card"> 64 +
65 - <div class="content"> 65 + <form class="content ui form" method="post" action="/post/addpost">
66 - <br><br> 66 + <h4 class="ui dividing header">글 쓰기 입력 항목</h4>
67 - <a class="ui button" href='/postadd'>글쓰기</a> 67 +
68 - 68 + <div class="two fields">
69 - </div> 69 + <div class="field">
70 - </div> 70 + <label>제목</label>
71 - </div> 71 + <input type="text" name="title" placeholder="제목 입력">
72 - </div> 72 + </div>
73 + <div class="field"></div>
74 + </div>
75 +
76 + <div class="field">
77 + <label>내용</label>
78 + <textarea id="contents" name="contents" rows="10" cols="80"></textarea>
79 + </div>
80 +
81 + <div class="two fields">
82 + <div class="field">
83 + <label>작성자</label>
84 + <input type="text" name="writer" placeholder="작성자아이디 입력">
85 + </div>
86 + <div class="field"></div>
87 + </div>
88 +
89 + <input class="ui submit button" type="submit" value="추가" name="" />
90 + <input class="ui button" type="button" value="취소" name="" onclick="javascript:history.back()" />
91 + </form>
92 +
93 + <br><br>
94 +
95 + </div>
96 + </div>
97 + </div>
98 +
73 </main> 99 </main>
74 <!-- Footer--> 100 <!-- Footer-->
75 <footer class="bg-dark py-4 mt-auto"> 101 <footer class="bg-dark py-4 mt-auto">
......
1 +<!DOCTYPE html>
2 +<html lang="en">
3 +
4 +<head>
5 + <meta charset="utf-8" />
6 + <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
7 + <meta name="description" content="" />
8 + <meta name="author" content="" />
9 + <title>Modern Business - Start Bootstrap Template</title>
10 + <!-- Favicon-->
11 + <link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
12 + <!-- Bootstrap icons-->
13 + <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.5.0/font/bootstrap-icons.css" rel="stylesheet" />
14 + <!-- Core theme CSS (includes Bootstrap)-->
15 + <link href="css/styles.css" rel="stylesheet" />
16 + <script src="http://code.jquery.com/jquery-2.1.4.js"></script>
17 + <script>
18 + $(document).ready(function() {
19 + var output = $("#contentsOutput").text();
20 + console.log('output : ' + output);
21 +
22 + $("#contentsOutput").html(output);
23 +
24 + });
25 + </script>
26 +</head>
27 +
28 +<body class="d-flex flex-column h-100">
29 + <main class="flex-shrink-0">
30 + <!-- Navigation-->
31 + <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
32 + <div class="container px-5">
33 + <a class="navbar-brand" href="/">휴게소 정보</a>
34 + <button class="navbar-toggler" type="button" data-bs-toggle="collapse"
35 + data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"
36 + aria-expanded="false" aria-label="Toggle navigation"><span
37 + class="navbar-toggler-icon"></span></button>
38 + <div class="collapse navbar-collapse" id="navbarSupportedContent">
39 + <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
40 + <li class="nav-item"><a class="nav-link" href="/">Home</a></li>
41 + <li class="nav-item"><a class="nav-link" href="/menu">휴게소 메뉴</a></li>
42 + <li class="nav-item"><a class="nav-link" href="/weather">날씨</a></li>
43 + <li class="nav-item"><a class="nav-link" href="/lpg">LPG</a></li>
44 + <li class="nav-item"><a class="nav-link" href="/post">Board</a></li>
45 + <li class="nav-item dropdown">
46 + <a class="nav-link dropdown-toggle" id="navbarDropdownBlog" href="#" role="button"
47 + data-bs-toggle="dropdown" aria-expanded="false">Blog</a>
48 + <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdownBlog">
49 + <li><a class="dropdown-item" href="blog-home.html">Blog Home</a></li>
50 + <li><a class="dropdown-item" href="blog-post.html">Blog Post</a></li>
51 + </ul>
52 + </li>
53 + <li class="nav-item dropdown">
54 + <a class="nav-link dropdown-toggle" id="navbarDropdownLogin" href="#" role="button"
55 + data-bs-toggle="dropdown" aria-expanded="false">Login</a>
56 + <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdownLogin">
57 + <li><a class="dropdown-item" href="/login">Login</a></li>
58 + <li><a class="dropdown-item" href="/signup">Sign-up</a></li>
59 + </ul>
60 + </li>
61 + </ul>
62 + </div>
63 + </div>
64 + </nav>
65 + <!-- Page Content-->
66 + <div class="ui raised segment">
67 + <a class="ui blue ribbon label">게시판</a>
68 + <span id="board_title"><%=title %></span>
69 +
70 +
71 + <div class="ui blue fluid card">
72 + <div class="content">
73 + <%
74 + var curTitle = posts._doc.title;
75 + var curContents = posts._doc.contents;
76 + var curWriter = "admin";
77 +
78 + var entities = new Entities();
79 +
80 + var decodedContents = entities.decode(curContents);
81 + %>
82 +
83 + <div id="titleOutput" class="header"><%=curTitle %></div>
84 + <div class="meta">
85 + <span class="right floated time">1일전</span>
86 + <span class="category">일반</span>
87 + </div>
88 + <div id="contentsOutput" class="description">
89 + <%=decodedContents %>
90 + </div>
91 + </div>
92 + <div class="extra content">
93 + <div class="right floated author">
94 + <%=curWriter %>
95 + </div>
96 + </div>
97 +
98 + </div>
99 +
100 + <br><br><a class="ui button" href='/post'>글 목록</a>
101 +
102 + </div>
103 +
104 + </main>
105 + <!-- Footer-->
106 + <footer class="bg-dark py-4 mt-auto">
107 + <div class="container px-5">
108 + <div class="row align-items-center justify-content-between flex-column flex-sm-row">
109 + <div class="col-auto">
110 + <div class="small m-0 text-white">Copyright &copy; Your Website 2022</div>
111 + </div>
112 + <div class="col-auto">
113 + <a class="link-light small" href="#!">Privacy</a>
114 + <span class="text-white mx-1">&middot;</span>
115 + <a class="link-light small" href="#!">Terms</a>
116 + <span class="text-white mx-1">&middot;</span>
117 + <a class="link-light small" href="#!">Contact</a>
118 + </div>
119 + </div>
120 + </div>
121 + </footer>
122 + <!-- Bootstrap core JS-->
123 + <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
124 + <!-- Core theme JS-->
125 + <script src="js/scripts.js"></script>
126 +</body>
127 +
128 +</html>
...\ No newline at end of file ...\ No newline at end of file
...@@ -555,9 +555,9 @@ ...@@ -555,9 +555,9 @@
555 } 555 }
556 }, 556 },
557 "node_modules/html-entities": { 557 "node_modules/html-entities": {
558 - "version": "2.3.3", 558 + "version": "1.4.0",
559 - "resolved": "https://registry.npmjs.org/html-entities/-/html-entities-2.3.3.tgz", 559 + "resolved": "https://registry.npmjs.org/html-entities/-/html-entities-1.4.0.tgz",
560 - "integrity": "sha512-DV5Ln36z34NNTDgnz0EWGBLZENelNAtkiFA4kyNOG2tDI6Mz1uSWiq1wAKdyjnJwyDiDO7Fa2SO1CTxPXL8VxA==" 560 + "integrity": "sha512-8nxjcBcd8wovbeKx7h3wTji4e6+rhaVuPNpMqwWgnHh+N9ToqsCs6XztWRBPQ+UtzsoMAdKZtUENoVzU/EMtZA=="
561 }, 561 },
562 "node_modules/http": { 562 "node_modules/http": {
563 "version": "0.0.1-security", 563 "version": "0.0.1-security",
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
17 "express": "^4.18.1", 17 "express": "^4.18.1",
18 "express-error-handler": "^1.1.0", 18 "express-error-handler": "^1.1.0",
19 "express-session": "^1.17.3", 19 "express-session": "^1.17.3",
20 - "html-entities": "^2.3.3", 20 + "html-entities": "^1.2.0",
21 "http": "^0.0.1-security", 21 "http": "^0.0.1-security",
22 "mongoose": "^6.3.4", 22 "mongoose": "^6.3.4",
23 "mysql": "^2.18.1", 23 "mysql": "^2.18.1",
...@@ -576,9 +576,9 @@ ...@@ -576,9 +576,9 @@
576 } 576 }
577 }, 577 },
578 "node_modules/html-entities": { 578 "node_modules/html-entities": {
579 - "version": "2.3.3", 579 + "version": "1.4.0",
580 - "resolved": "https://registry.npmjs.org/html-entities/-/html-entities-2.3.3.tgz", 580 + "resolved": "https://registry.npmjs.org/html-entities/-/html-entities-1.4.0.tgz",
581 - "integrity": "sha512-DV5Ln36z34NNTDgnz0EWGBLZENelNAtkiFA4kyNOG2tDI6Mz1uSWiq1wAKdyjnJwyDiDO7Fa2SO1CTxPXL8VxA==" 581 + "integrity": "sha512-8nxjcBcd8wovbeKx7h3wTji4e6+rhaVuPNpMqwWgnHh+N9ToqsCs6XztWRBPQ+UtzsoMAdKZtUENoVzU/EMtZA=="
582 }, 582 },
583 "node_modules/http": { 583 "node_modules/http": {
584 "version": "0.0.1-security", 584 "version": "0.0.1-security",
...@@ -1706,9 +1706,9 @@ ...@@ -1706,9 +1706,9 @@
1706 "integrity": "sha512-l3LCuF6MgDNwTDKkdYGEihYjt5pRPbEg46rtlmnSPlUbgmB8LOIrKJbYYFBSbnPaJexMKtiPO8hmeRjRz2Td+A==" 1706 "integrity": "sha512-l3LCuF6MgDNwTDKkdYGEihYjt5pRPbEg46rtlmnSPlUbgmB8LOIrKJbYYFBSbnPaJexMKtiPO8hmeRjRz2Td+A=="
1707 }, 1707 },
1708 "html-entities": { 1708 "html-entities": {
1709 - "version": "2.3.3", 1709 + "version": "1.4.0",
1710 - "resolved": "https://registry.npmjs.org/html-entities/-/html-entities-2.3.3.tgz", 1710 + "resolved": "https://registry.npmjs.org/html-entities/-/html-entities-1.4.0.tgz",
1711 - "integrity": "sha512-DV5Ln36z34NNTDgnz0EWGBLZENelNAtkiFA4kyNOG2tDI6Mz1uSWiq1wAKdyjnJwyDiDO7Fa2SO1CTxPXL8VxA==" 1711 + "integrity": "sha512-8nxjcBcd8wovbeKx7h3wTji4e6+rhaVuPNpMqwWgnHh+N9ToqsCs6XztWRBPQ+UtzsoMAdKZtUENoVzU/EMtZA=="
1712 }, 1712 },
1713 "http": { 1713 "http": {
1714 "version": "0.0.1-security", 1714 "version": "0.0.1-security",
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
17 "express": "^4.18.1", 17 "express": "^4.18.1",
18 "express-error-handler": "^1.1.0", 18 "express-error-handler": "^1.1.0",
19 "express-session": "^1.17.3", 19 "express-session": "^1.17.3",
20 - "html-entities": "^2.3.3", 20 + "html-entities": "^1.2.0",
21 "http": "^0.0.1-security", 21 "http": "^0.0.1-security",
22 "mongoose": "^6.3.4", 22 "mongoose": "^6.3.4",
23 "mysql": "^2.18.1", 23 "mysql": "^2.18.1",
......