Showing
1 changed file
with
87 additions
and
0 deletions
app2.js
0 → 100644
| 1 | +var createError = require('http-errors'); | ||
| 2 | +var express = require('express'); | ||
| 3 | +var path = require('path'); | ||
| 4 | +var cookieParser = require('cookie-parser'); | ||
| 5 | +var logger = require('morgan'); | ||
| 6 | +var app = express(); | ||
| 7 | +const mongodb = require('mongodb'); | ||
| 8 | +const MongoClient = mongodb.MongoClient; | ||
| 9 | + | ||
| 10 | +const url = 'mongodb+srv://hellowhales:qogudtjr`12@cluster0.7gz7l.mongodb.net/myFirstDatabase?retryWrites=true&w=majority'; | ||
| 11 | + | ||
| 12 | +let db; | ||
| 13 | + | ||
| 14 | +app.use(express.urlencoded({ extended: true })) | ||
| 15 | + | ||
| 16 | +// 터미널창 연결방법 mongo 'mongodb+srv://hellowhales:qogudtjr`12@cluster0.7gz7l.mongodb.net/myFirstDatabase?retryWrites=true&w=majority'; | ||
| 17 | +// db.festivals.find({"title":"가무악극으로 만나는 토요 상설공연"}) 검색방법 | ||
| 18 | + | ||
| 19 | +//https://www.mongodb.com/try/download/database-tools?tck=docs_databasetools 여기서 mongoexport 실행을 위한 tool 다운받아서 Program files/mongodb/bin 폴더 안에 넣어줘야 함 | ||
| 20 | + | ||
| 21 | +MongoClient.connect(url, (error, client) => { // 서버열때 url 사용 mongoDB와 연결시키기 | ||
| 22 | + if (error) return console.log(error); | ||
| 23 | + db = client.db('myFirstDatabase'); // 클러스터의 데이터베이스를 db변수에 저장 | ||
| 24 | + app.listen(3001, () => { | ||
| 25 | + console.log('3001 port on'); | ||
| 26 | + }); | ||
| 27 | +}); | ||
| 28 | + | ||
| 29 | +app.get('/festivalList', (req, res) => { // localhost:3000/festivalList 입력하면 list.ejs에 저장한 형식대로 정보 불러와짐 | ||
| 30 | + //디비에 저장된 festivals 라는 collection안의 데이터(제목 또는 내용 등)를 꺼내기 | ||
| 31 | + db.collection('festivals').find().toArray((err, rslt) => { //DB에서 데이터를 찾음 festivals라는 collection안의 데이터를 꺼내게 됨 | ||
| 32 | + if (err) throw err; | ||
| 33 | + console.log(rslt); | ||
| 34 | + res.render('list.ejs', { posts: rslt }); // 찾은 데이터를 ejs 파일에 넣음 | ||
| 35 | + }); | ||
| 36 | +}); | ||
| 37 | + | ||
| 38 | + | ||
| 39 | + | ||
| 40 | +// 서버 연결하고 mongoexport 설치 후 mongoexport -d myFirstDatabase -c festivals -o festivalList.json /pretty 'mongodb+srv://hellowhales:qogudtjr`12@cluster0.7gz7l.mongodb.net/myFirstDatabase?retryWrites=true&w=majority' 명령 | ||
| 41 | +// 이용해서 myFirstDatabase라는 Db 안에서 festivals라는 collection 안의 데이터를 festivalList.json 파일로 export 해옴. | ||
| 42 | + | ||
| 43 | + | ||
| 44 | +var indexRouter = require('./routes/index'); | ||
| 45 | +var usersRouter = require('./routes/users'); | ||
| 46 | +const { mongo } = require('mongoose'); | ||
| 47 | + | ||
| 48 | + | ||
| 49 | + | ||
| 50 | +// view engine setup | ||
| 51 | + | ||
| 52 | +app.set('views', path.join(__dirname, 'views')); | ||
| 53 | +app.set('view engine', 'ejs'); // express에서 view엔진을 ejs로 설정하는과정 | ||
| 54 | + | ||
| 55 | + | ||
| 56 | + | ||
| 57 | +app.use(logger('dev')); | ||
| 58 | +app.use(express.json()); | ||
| 59 | +app.use(express.urlencoded({ extended: false })); | ||
| 60 | +app.use(cookieParser()); | ||
| 61 | +app.use(express.static(path.join(__dirname, 'public'))); | ||
| 62 | + | ||
| 63 | +app.use('/', indexRouter); | ||
| 64 | +app.use('/users', usersRouter); | ||
| 65 | + | ||
| 66 | +// catch 404 and forward to error handler | ||
| 67 | +app.use(function (req, res, next) { | ||
| 68 | + next(createError(404)); | ||
| 69 | +}); | ||
| 70 | + | ||
| 71 | +// error handler | ||
| 72 | +app.use(function (err, req, res, next) { | ||
| 73 | + // set locals, only providing error in development | ||
| 74 | + res.locals.message = err.message; | ||
| 75 | + res.locals.error = req.app.get('env') === 'development' ? err : {}; | ||
| 76 | + | ||
| 77 | + // render the error page | ||
| 78 | + res.status(err.status || 500); | ||
| 79 | + res.render('error'); | ||
| 80 | +}); | ||
| 81 | + | ||
| 82 | + | ||
| 83 | + | ||
| 84 | + | ||
| 85 | +module.exports = app; | ||
| 86 | + | ||
| 87 | +//유저 로그인할때 모든 여행지에 대한 정보를 DB에서 싹다 불러옴 페이지 렌더링할때 data 뿌려줌 | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment