최은석

mongoDB post save test

...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
6 "@testing-library/jest-dom": "^5.16.4", 6 "@testing-library/jest-dom": "^5.16.4",
7 "@testing-library/react": "^13.3.0", 7 "@testing-library/react": "^13.3.0",
8 "@testing-library/user-event": "^13.5.0", 8 "@testing-library/user-event": "^13.5.0",
9 + "axios": "^0.27.2",
9 "react": "^18.1.0", 10 "react": "^18.1.0",
10 "react-dom": "^18.1.0", 11 "react-dom": "^18.1.0",
11 "react-scripts": "5.0.1", 12 "react-scripts": "5.0.1",
......
This diff is collapsed. Click to expand it.
1 { 1 {
2 "scripts": { 2 "scripts": {
3 "server": "cd server && nodemon server", 3 "server": "cd server && nodemon server",
4 - "client": "cd client && npm start", 4 + "client": "cd client && npm start --port",
5 "start": "concurrently --kill-others-on-fail \"npm run server\" \"npm run client\"" 5 "start": "concurrently --kill-others-on-fail \"npm run server\" \"npm run client\""
6 }, 6 },
7 "dependencies": { 7 "dependencies": {
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
9 "concurrently": "^7.2.1", 9 "concurrently": "^7.2.1",
10 "express": "^4.18.1", 10 "express": "^4.18.1",
11 "http-proxy-middleware": "^2.0.6", 11 "http-proxy-middleware": "^2.0.6",
12 + "mongoose": "^6.3.4",
12 "nodemon": "^2.0.16" 13 "nodemon": "^2.0.16"
13 } 14 }
14 } 15 }
......
1 +const express = require('express');
2 +const router = express.Router();
3 +const mongoose = require('mongoose')
4 +
5 +mongoose.connect('mongodb+srv://choieunseok:uA3mhjPcB3DwsuuD@cluster0.2gsua4u.mongodb.net/?retryWrites=true&w=majority');
6 +
7 +const db = mongoose.connection
8 +db.on('error', console.error)
9 +db.once('open', () => {
10 + console.log('Connected to mongodb Server')
11 +});
12 +
13 +const dayPostList = mongoose.Schema({
14 + date: 'string',
15 + idArray: [{ type: String }]
16 +});
17 +const dayPostListModel = mongoose.model('dayPostList', dayPostList);
18 +const post = mongoose.Schema({
19 + date: 'string',
20 + title: 'string',
21 + content: 'string',
22 + password: 'string'
23 +});
24 +const postModel = mongoose.model('post', post);
25 +
26 +// router.get('/api', (req, res) => {
27 +// res.send({ test: "hi" });
28 +// });
29 +
30 +router.get('/api/getAll', (req, res) => {
31 + dayPostListModel.find(function (error, dayPostLists) {
32 + console.log('--- Read all ---');
33 + if (error) {
34 + res.send(error);
35 + } else {
36 + res.send(dayPostLists);
37 + }
38 + })
39 +});
40 +
41 +router.get('/api/testSave', async(req, res) => {
42 + var isFirst = false;
43 +
44 + var testDayPostList = await dayPostListModel.findOne({date: '2022-05-30'});
45 + if(testDayPostList == null){
46 + testDayPostList = new dayPostListModel({ date: '2022-05-30', idArray: [] });
47 + isFirst = true;
48 + }
49 + var postListArr = testDayPostList.idArray;
50 +
51 + var newPost = new postModel({ date: '2022-05-30', title: '테스트 제목', age: '테스트 내용', password: 'password' });
52 + var newPostData = await newPost.save();
53 + postListArr.push(newPostData._id.toString());
54 +
55 + if(isFirst) await testDayPostList.save();
56 + else await dayPostListModel.updateOne({date: '2022-05-30'},{idArray: postListArr});
57 +
58 + res.send("test");
59 +});
60 +
61 +module.exports = router;
62 +
1 -const express = require('express');
2 -const router = express.Router();
3 -
4 -router.get('/api', (req, res)=>{
5 - res.send({ test: "hi"});
6 -});
7 -
8 -module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
1 const express = require('express'); 1 const express = require('express');
2 const app = express(); 2 const app = express();
3 -const test = require('.//Router/test'); 3 +const api = require('./Router/api');
4 4
5 -app.use('/', test); 5 +app.use('/', api);
6 6
7 -const port=23023;
8 -app.listen(port, ()=>{console.log(`Listening on port ${port}`)});
...\ No newline at end of file ...\ No newline at end of file
7 +const port = 23023;
8 +app.listen(port, () => { console.log(`Listening on port ${port}`) });
...\ No newline at end of file ...\ No newline at end of file
......