Toggle navigation
Toggle navigation
This project
Loading...
Sign in
최은석
/
ossw-project
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
최은석
2022-05-30 23:21:23 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
915c7c6d20b6238c991d21bae21936089c305086
915c7c6d
1 parent
c143bf3c
/api/getList, /api/getList/:date, /api/get, /api/get/:id, /api/isPassEqual, /api/postSave
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
90 additions
and
17 deletions
package-lock.json
package.json
server/Router/api.js
server/server.js
package-lock.json
View file @
915c7c6
...
...
@@ -6,6 +6,7 @@
""
:
{
"dependencies"
:
{
"axios"
:
"^0.27.2"
,
"body-parser"
:
"^1.20.0"
,
"concurrently"
:
"^7.2.1"
,
"express"
:
"^4.18.1"
,
"http-proxy-middleware"
:
"^2.0.6"
,
...
...
package.json
View file @
915c7c6
...
...
@@ -6,6 +6,7 @@
},
"dependencies"
:
{
"axios"
:
"^0.27.2"
,
"body-parser"
:
"^1.20.0"
,
"concurrently"
:
"^7.2.1"
,
"express"
:
"^4.18.1"
,
"http-proxy-middleware"
:
"^2.0.6"
,
...
...
server/Router/api.js
View file @
915c7c6
...
...
@@ -27,36 +27,104 @@ const postModel = mongoose.model('post', post);
// res.send({ test: "hi" });
// });
router
.
get
(
'/api/getAll'
,
(
req
,
res
)
=>
{
dayPostListModel
.
find
(
function
(
error
,
dayPostLists
)
{
console
.
log
(
'--- Read all ---'
);
if
(
error
)
{
res
.
send
(
error
);
}
else
{
res
.
send
(
dayPostLists
);
function
getCurrentDate
(
originDate
)
{
var
date
;
if
(
originDate
==
null
)
date
=
new
Date
();
else
date
=
new
Date
(
originDate
);
var
year
=
date
.
getFullYear
().
toString
();
var
month
=
date
.
getMonth
()
+
1
;
month
=
month
<
10
?
'0'
+
month
.
toString
()
:
month
.
toString
();
var
day
=
date
.
getDate
();
day
=
day
<
10
?
'0'
+
day
.
toString
()
:
day
.
toString
();
return
year
+
'-'
+
month
+
'-'
+
day
;
}
router
.
get
(
'/api/getList'
,
async
(
req
,
res
)
=>
{
const
today
=
getCurrentDate
();
var
testDayPostList
=
await
dayPostListModel
.
findOne
({
date
:
today
});
if
(
testDayPostList
==
null
)
testDayPostList
=
new
dayPostListModel
({
date
:
today
,
idArray
:
[]
});
res
.
send
(
testDayPostList
.
idArray
);
});
router
.
get
(
'/api/getList/:date'
,
async
(
req
,
res
)
=>
{
const
today
=
getCurrentDate
(
req
.
params
.
date
);
var
testDayPostList
=
await
dayPostListModel
.
findOne
({
date
:
today
});
if
(
testDayPostList
==
null
)
testDayPostList
=
new
dayPostListModel
({
date
:
today
,
idArray
:
[]
});
res
.
send
(
testDayPostList
.
idArray
);
});
router
.
get
(
'/api/get'
,
async
(
req
,
res
)
=>
{
const
idArray
=
req
.
body
.
idArray
;
var
resultArray
=
[];
for
(
const
id
of
idArray
){
const
onePost
=
await
postModel
.
findById
(
id
);
var
tempJSON
=
{};
tempJSON
.
id
=
onePost
.
id
;
tempJSON
.
title
=
onePost
.
title
;
tempJSON
.
content
=
onePost
.
content
;
tempJSON
.
content
=
tempJSON
.
content
.
replace
(
/
(?:\r\n
|
\r
|
\n)
/g
,
''
);
const
sliceLength
=
10
;
if
(
tempJSON
.
content
.
length
>
sliceLength
)
tempJSON
.
content
=
tempJSON
.
content
.
slice
(
0
,
sliceLength
)
+
"..."
;
resultArray
.
push
(
tempJSON
);
}
})
res
.
send
(
resultArray
);
});
router
.
get
(
'/api/get/:id'
,
async
(
req
,
res
)
=>
{
const
currentPost
=
await
postModel
.
findById
(
req
.
params
.
id
);
res
.
send
({
title
:
currentPost
.
title
,
content
:
currentPost
.
content
});
});
router
.
get
(
'/api/testSave'
,
async
(
req
,
res
)
=>
{
router
.
post
(
'/api/isPassEqual'
,
async
(
req
,
res
)
=>
{
const
currentPost
=
await
postModel
.
findById
(
req
.
body
.
id
);
if
(
currentPost
.
password
==
req
.
body
.
password
)
res
.
send
(
"success"
);
else
res
.
send
(
"failed"
);
});
router
.
post
(
'/api/postSave'
,
async
(
req
,
res
)
=>
{
var
isFirst
=
false
;
const
today
=
getCurrentDate
();
var
testDayPostList
=
await
dayPostListModel
.
findOne
({
date
:
'2022-05-30'
});
if
(
testDayPostList
==
null
)
{
testDayPostList
=
new
dayPostListModel
({
date
:
'2022-05-30'
,
idArray
:
[]
});
var
testDayPostList
=
await
dayPostListModel
.
findOne
({
date
:
today
});
if
(
testDayPostList
==
null
)
{
testDayPostList
=
new
dayPostListModel
({
date
:
today
,
idArray
:
[]
});
isFirst
=
true
;
}
var
postListArr
=
testDayPostList
.
idArray
;
var
newPost
=
new
postModel
({
date
:
'2022-05-30'
,
title
:
'테스트 제목'
,
age
:
'테스트 내용'
,
password
:
'password'
});
var
newPost
=
new
postModel
({
date
:
today
,
title
:
req
.
body
.
title
,
content
:
req
.
body
.
content
,
password
:
req
.
body
.
password
});
var
newPostData
=
await
newPost
.
save
();
postListArr
.
push
(
newPostData
.
_id
.
toString
());
if
(
isFirst
)
await
testDayPostList
.
save
();
else
await
dayPostListModel
.
updateOne
({
date
:
'2022-05-30'
},{
idArray
:
postListArr
});
if
(
isFirst
)
await
testDayPostList
.
save
();
else
await
dayPostListModel
.
updateOne
({
date
:
today
},
{
idArray
:
postListArr
});
res
.
send
(
"test"
);
res
.
send
(
newPostData
);
});
// 게시물 수정, 삭제 추가예정 ---------------------------------------------------------------------------------------------------------------------------------------
// router.get('/api/testSave', async (req, res) => {
// var isFirst = false;
// var testDayPostList = await dayPostListModel.findOne({ date: '2022-05-30' });
// if (testDayPostList == null) {
// testDayPostList = new dayPostListModel({ date: '2022-05-30', idArray: [] });
// isFirst = true;
// }
// var postListArr = testDayPostList.idArray;
// var newPost = new postModel({ date: '2022-05-30', title: '테스트 제목', content: '테스트 내용', password: 'password' });
// var newPostData = await newPost.save();
// postListArr.push(newPostData._id.toString());
// if (isFirst) await testDayPostList.save();
// else await dayPostListModel.updateOne({ date: '2022-05-30' }, { idArray: postListArr });
// res.send("test");
// });
module
.
exports
=
router
;
...
...
server/server.js
View file @
915c7c6
const
express
=
require
(
'express'
);
const
app
=
express
();
const
api
=
require
(
'./Router/api'
);
let
bodyParser
=
require
(
'body-parser'
);
app
.
use
(
bodyParser
.
urlencoded
({
extended
:
false
}));
app
.
use
(
bodyParser
.
json
());
app
.
use
(
'/'
,
api
);
...
...
Please
register
or
login
to post a comment