Showing
1 changed file
with
115 additions
and
2 deletions
| ... | @@ -4,15 +4,127 @@ var path = require('path'); | ... | @@ -4,15 +4,127 @@ 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 | ||
| 7 | +mongoose.connect('mongodb://root:root@localhost:27017/travelDB', { | ||
| 8 | + dbName: 'travelDB', | ||
| 9 | + useNewUrlParser: true, | ||
| 10 | + useUnifiedTopology: true | ||
| 11 | +}); // node앱을 mongoDB에 연결 | ||
| 12 | +//mongoDB express를 실행했을 때, username = root, password = root, Database = travelDB를 입력해서 연결 가능 | ||
| 13 | + | ||
| 7 | var indexRouter = require('./routes/index'); | 14 | var indexRouter = require('./routes/index'); |
| 8 | var usersRouter = require('./routes/users'); | 15 | var usersRouter = require('./routes/users'); |
| 9 | 16 | ||
| 10 | var app = express(); | 17 | var app = express(); |
| 11 | 18 | ||
| 12 | // view engine setup | 19 | // view engine setup |
| 20 | + | ||
| 13 | app.set('views', path.join(__dirname, 'views')); | 21 | app.set('views', path.join(__dirname, 'views')); |
| 14 | app.set('view engine', 'ejs'); | 22 | app.set('view engine', 'ejs'); |
| 15 | 23 | ||
| 24 | + | ||
| 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 | + | ||
| 16 | app.use(logger('dev')); | 128 | app.use(logger('dev')); |
| 17 | app.use(express.json()); | 129 | app.use(express.json()); |
| 18 | app.use(express.urlencoded({ extended: false })); | 130 | app.use(express.urlencoded({ extended: false })); |
| ... | @@ -23,12 +135,12 @@ app.use('/', indexRouter); | ... | @@ -23,12 +135,12 @@ app.use('/', indexRouter); |
| 23 | app.use('/users', usersRouter); | 135 | app.use('/users', usersRouter); |
| 24 | 136 | ||
| 25 | // catch 404 and forward to error handler | 137 | // catch 404 and forward to error handler |
| 26 | -app.use(function(req, res, next) { | 138 | +app.use(function (req, res, next) { |
| 27 | next(createError(404)); | 139 | next(createError(404)); |
| 28 | }); | 140 | }); |
| 29 | 141 | ||
| 30 | // error handler | 142 | // error handler |
| 31 | -app.use(function(err, req, res, next) { | 143 | +app.use(function (err, req, res, next) { |
| 32 | // set locals, only providing error in development | 144 | // set locals, only providing error in development |
| 33 | res.locals.message = err.message; | 145 | res.locals.message = err.message; |
| 34 | res.locals.error = req.app.get('env') === 'development' ? err : {}; | 146 | res.locals.error = req.app.get('env') === 'development' ? err : {}; |
| ... | @@ -38,4 +150,5 @@ app.use(function(err, req, res, next) { | ... | @@ -38,4 +150,5 @@ app.use(function(err, req, res, next) { |
| 38 | res.render('error'); | 150 | res.render('error'); |
| 39 | }); | 151 | }); |
| 40 | 152 | ||
| 153 | + | ||
| 41 | module.exports = app; | 154 | module.exports = app; | ... | ... |
-
Please register or login to post a comment