Showing
1 changed file
with
26 additions
and
110 deletions
| ... | @@ -3,18 +3,28 @@ var express = require('express'); | ... | @@ -3,18 +3,28 @@ var express = require('express'); |
| 3 | var path = require('path'); | 3 | var path = require('path'); |
| 4 | var cookieParser = require('cookie-parser'); | 4 | var cookieParser = require('cookie-parser'); |
| 5 | var logger = require('morgan'); | 5 | var logger = require('morgan'); |
| 6 | - | 6 | +var app = express(); |
| 7 | -mongoose.connect('mongodb://root:root@localhost:27017/travelDB', { | 7 | +const mongodb = require('mongodb'); |
| 8 | - dbName: 'travelDB', | 8 | +const MongoClient = mongodb.MongoClient; |
| 9 | - useNewUrlParser: true, | 9 | +const url = 'mongodb+srv://hellowhales:qogudtjr`12@cluster0.7gz7l.mongodb.net/myFirstDatabase?retryWrites=true&w=majority'; |
| 10 | - useUnifiedTopology: true | 10 | + |
| 11 | -}); // node앱을 mongoDB에 연결 | 11 | +// 터미널창 연결방법 mongo 'mongodb+srv://hellowhales:qogudtjr`12@cluster0.7gz7l.mongodb.net/myFirstDatabase?retryWrites=true&w=majority'; |
| 12 | -//mongoDB express를 실행했을 때, username = root, password = root, Database = travelDB를 입력해서 연결 가능 | 12 | +// db.festivals.find({"title":"가무악극으로 만나는 토요 상설공연"}) 검색방법 |
| 13 | + | ||
| 14 | +MongoClient.connect(url) //서버 열때 mongodb와 연결시키기 | ||
| 15 | + .then(client => { | ||
| 16 | + console.log('mongo connected'); | ||
| 17 | + console.log(client); | ||
| 18 | + }) | ||
| 19 | + .then(app.listen(3000, () => { | ||
| 20 | + console.log('3000 port on'); | ||
| 21 | + })) | ||
| 22 | + .catch(err => console.log(err)); | ||
| 13 | 23 | ||
| 14 | var indexRouter = require('./routes/index'); | 24 | var indexRouter = require('./routes/index'); |
| 15 | var usersRouter = require('./routes/users'); | 25 | var usersRouter = require('./routes/users'); |
| 16 | 26 | ||
| 17 | -var app = express(); | 27 | + |
| 18 | 28 | ||
| 19 | // view engine setup | 29 | // view engine setup |
| 20 | 30 | ||
| ... | @@ -22,108 +32,6 @@ app.set('views', path.join(__dirname, 'views')); | ... | @@ -22,108 +32,6 @@ app.set('views', path.join(__dirname, 'views')); |
| 22 | app.set('view engine', 'ejs'); | 32 | app.set('view engine', 'ejs'); |
| 23 | 33 | ||
| 24 | 34 | ||
| 25 | -var TravelSchema = mongoose.Schema({ //Travel이라는 스키마 생성(javascript 객체 생성자와 유사) | ||
| 26 | - name: String, | ||
| 27 | - address: String, | ||
| 28 | - weather: String, | ||
| 29 | - date: String, | ||
| 30 | - url: String, | ||
| 31 | - pos: String | ||
| 32 | -}); | ||
| 33 | - | ||
| 34 | -const toTravel = mongoose.model('Travel', TravelSchema); // 스키마를 model로 정의 -> collection 명칭을 mongoose에서는 단수로 사용하지만 | ||
| 35 | -// mongodb에 저장될 때에는 복수 + 소문자로 저장됨 여기선 travels로 저장되므로 db.travels라고 호출해야 정상적인 값 얻기 가능 | ||
| 36 | -// db.travels.find()처럼 사용 | ||
| 37 | -var JeonJu = new toTravel({ | ||
| 38 | - name: '전주 한옥마을', | ||
| 39 | - address: '전라북도 전주시 완산구 풍남동3가 기린대로 99', | ||
| 40 | - weather: '맑음', | ||
| 41 | - date: '2021-11-14' | ||
| 42 | -}); | ||
| 43 | - | ||
| 44 | -var HongDae = new toTravel({ | ||
| 45 | - name: '서울 홍대거리', | ||
| 46 | - address: '서울특별시 마포구 와우산로 94(상수동) 홍익대학교', | ||
| 47 | - weather: '흐림', | ||
| 48 | - date: '2021-11-15' | ||
| 49 | -}); | ||
| 50 | -var Busan = new toTravel({ | ||
| 51 | - name: '부산 해운대', | ||
| 52 | - address: '부산광역시 해운대구 중동', | ||
| 53 | - weather: '우박', | ||
| 54 | - date: '2021-11-16' | ||
| 55 | -}); | ||
| 56 | -var Jeju = new toTravel({ | ||
| 57 | - name: '제주도', | ||
| 58 | - address: '제주특별자치도 제주시 세종로 88', | ||
| 59 | - weather: '비', | ||
| 60 | - date: '2021-11-17' | ||
| 61 | -}); | ||
| 62 | -// Travel 객체를 new로 생성해서 값을 입력해줌 | ||
| 63 | - | ||
| 64 | -JeonJu.save(function (error, data) { | ||
| 65 | - if (error) { | ||
| 66 | - console.log(error); | ||
| 67 | - } | ||
| 68 | - else { | ||
| 69 | - console.log('saved!'); | ||
| 70 | - } | ||
| 71 | -}); | ||
| 72 | - | ||
| 73 | -toTravel.insertMany([Seoul, Busan, Jeju], function (err) { | ||
| 74 | - if (err) { | ||
| 75 | - console.log(err); | ||
| 76 | - } else { | ||
| 77 | - mongoose.connection.close(); | ||
| 78 | - console.log("Successfully saved to travelDB"); | ||
| 79 | - } | ||
| 80 | -}); | ||
| 81 | - | ||
| 82 | -toTravel.find(function (err, travels) { // toTravel이라는 모델에 저장된 데이터 전체 불러오기 = find(), 데이터 하나 가져오기 = findOne() | ||
| 83 | - console.log('---READ ALL---'); | ||
| 84 | - if (err) { | ||
| 85 | - console.log(err); | ||
| 86 | - } else { | ||
| 87 | - fruits.forEach(function (element) { | ||
| 88 | - console.log(travels.name); // 각각의 이름(지명) 출력 | ||
| 89 | - }); | ||
| 90 | - } | ||
| 91 | -}); | ||
| 92 | - | ||
| 93 | -const q0 = toTravel.find(); | ||
| 94 | - | ||
| 95 | -toTravel.updateOne({ // mongoDB에 저장된 document 내용 수정, 첫번째 파라미터로 수정할 document 선택, 두 번째 파라미터로 수정할 내용을 입력 | ||
| 96 | - _id: "5e859d8cef3a5d234c6c19c0" | ||
| 97 | -}, { | ||
| 98 | - name: "제주도" | ||
| 99 | -}, function (err) { | ||
| 100 | - if (err) { | ||
| 101 | - console.log(err); | ||
| 102 | - } else { | ||
| 103 | - console.log("Successfully updated the document"); | ||
| 104 | - } | ||
| 105 | -}); | ||
| 106 | - | ||
| 107 | -toTravel.deleteOne({ // mongoDB에 저장된 document 삭제. 첫 번째 파라미터로 삭제할 document를 선택하여 삭제 | ||
| 108 | - _id: "5e86a7a216b28f3b08a88755" | ||
| 109 | -}, function (err) { | ||
| 110 | - if (err) { | ||
| 111 | - console.log(err); | ||
| 112 | - } else { | ||
| 113 | - console.log("Successfully deleted"); | ||
| 114 | - } | ||
| 115 | -}); | ||
| 116 | - | ||
| 117 | - | ||
| 118 | -toTravel.find(function (error, travels) { //전체 가져오기 | ||
| 119 | - console.log('--- Read all ---'); | ||
| 120 | - if (error) { | ||
| 121 | - console.log(error); | ||
| 122 | - } else { | ||
| 123 | - console.log(travels); | ||
| 124 | - } | ||
| 125 | -}); | ||
| 126 | - | ||
| 127 | 35 | ||
| 128 | app.use(logger('dev')); | 36 | app.use(logger('dev')); |
| 129 | app.use(express.json()); | 37 | app.use(express.json()); |
| ... | @@ -150,5 +58,13 @@ app.use(function (err, req, res, next) { | ... | @@ -150,5 +58,13 @@ app.use(function (err, req, res, next) { |
| 150 | res.render('error'); | 58 | res.render('error'); |
| 151 | }); | 59 | }); |
| 152 | 60 | ||
| 61 | +app.get('*', function (req, res) { | ||
| 62 | + //DB에서 json형태의 데이터를 유저가 접속하자마자 바로 불러와서 frontend에 뿌려주기 | ||
| 63 | + // 렌더링할 때 frontend로 변수 뿌려주기 | ||
| 64 | + res.render("asd.html"); | ||
| 65 | + | ||
| 66 | +}); | ||
| 153 | 67 | ||
| 154 | module.exports = app; | 68 | module.exports = app; |
| 69 | + | ||
| 70 | +//유저 로그인할때 모든 여행지에 대한 정보를 DB에서 싹다 불러옴 페이지 렌더링할때 data 뿌려줌 | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment