Toggle navigation
Toggle navigation
This project
Loading...
Sign in
LI WENHAO
/
TFT
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
Jungmin Park
2021-11-15 18:21:51 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
d06d16070ffdc3e5eeec6212750d8f783347c0fd
d06d1607
1 parent
e066520b
Link DB and Node.js
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
115 additions
and
2 deletions
app.js
app.js
View file @
d06d160
...
...
@@ -4,15 +4,127 @@ var path = require('path');
var
cookieParser
=
require
(
'cookie-parser'
);
var
logger
=
require
(
'morgan'
);
mongoose
.
connect
(
'mongodb://root:root@localhost:27017/travelDB'
,
{
dbName
:
'travelDB'
,
useNewUrlParser
:
true
,
useUnifiedTopology
:
true
});
// node앱을 mongoDB에 연결
//mongoDB express를 실행했을 때, username = root, password = root, Database = travelDB를 입력해서 연결 가능
var
indexRouter
=
require
(
'./routes/index'
);
var
usersRouter
=
require
(
'./routes/users'
);
var
app
=
express
();
// view engine setup
app
.
set
(
'views'
,
path
.
join
(
__dirname
,
'views'
));
app
.
set
(
'view engine'
,
'ejs'
);
var
TravelSchema
=
mongoose
.
Schema
({
//Travel이라는 스키마 생성(javascript 객체 생성자와 유사)
name
:
String
,
address
:
String
,
weather
:
String
,
date
:
String
,
url
:
String
,
pos
:
String
});
const
toTravel
=
mongoose
.
model
(
'Travel'
,
TravelSchema
);
// 스키마를 model로 정의 -> collection 명칭을 mongoose에서는 단수로 사용하지만
// mongodb에 저장될 때에는 복수 + 소문자로 저장됨 여기선 travels로 저장되므로 db.travels라고 호출해야 정상적인 값 얻기 가능
// db.travels.find()처럼 사용
var
JeonJu
=
new
toTravel
({
name
:
'전주 한옥마을'
,
address
:
'전라북도 전주시 완산구 풍남동3가 기린대로 99'
,
weather
:
'맑음'
,
date
:
'2021-11-14'
});
var
HongDae
=
new
toTravel
({
name
:
'서울 홍대거리'
,
address
:
'서울특별시 마포구 와우산로 94(상수동) 홍익대학교'
,
weather
:
'흐림'
,
date
:
'2021-11-15'
});
var
Busan
=
new
toTravel
({
name
:
'부산 해운대'
,
address
:
'부산광역시 해운대구 중동'
,
weather
:
'우박'
,
date
:
'2021-11-16'
});
var
Jeju
=
new
toTravel
({
name
:
'제주도'
,
address
:
'제주특별자치도 제주시 세종로 88'
,
weather
:
'비'
,
date
:
'2021-11-17'
});
// Travel 객체를 new로 생성해서 값을 입력해줌
JeonJu
.
save
(
function
(
error
,
data
)
{
if
(
error
)
{
console
.
log
(
error
);
}
else
{
console
.
log
(
'saved!'
);
}
});
toTravel
.
insertMany
([
Seoul
,
Busan
,
Jeju
],
function
(
err
)
{
if
(
err
)
{
console
.
log
(
err
);
}
else
{
mongoose
.
connection
.
close
();
console
.
log
(
"Successfully saved to travelDB"
);
}
});
toTravel
.
find
(
function
(
err
,
travels
)
{
// toTravel이라는 모델에 저장된 데이터 전체 불러오기 = find(), 데이터 하나 가져오기 = findOne()
console
.
log
(
'---READ ALL---'
);
if
(
err
)
{
console
.
log
(
err
);
}
else
{
fruits
.
forEach
(
function
(
element
)
{
console
.
log
(
travels
.
name
);
// 각각의 이름(지명) 출력
});
}
});
const
q0
=
toTravel
.
find
();
toTravel
.
updateOne
({
// mongoDB에 저장된 document 내용 수정, 첫번째 파라미터로 수정할 document 선택, 두 번째 파라미터로 수정할 내용을 입력
_id
:
"5e859d8cef3a5d234c6c19c0"
},
{
name
:
"제주도"
},
function
(
err
)
{
if
(
err
)
{
console
.
log
(
err
);
}
else
{
console
.
log
(
"Successfully updated the document"
);
}
});
toTravel
.
deleteOne
({
// mongoDB에 저장된 document 삭제. 첫 번째 파라미터로 삭제할 document를 선택하여 삭제
_id
:
"5e86a7a216b28f3b08a88755"
},
function
(
err
)
{
if
(
err
)
{
console
.
log
(
err
);
}
else
{
console
.
log
(
"Successfully deleted"
);
}
});
toTravel
.
find
(
function
(
error
,
travels
)
{
//전체 가져오기
console
.
log
(
'--- Read all ---'
);
if
(
error
)
{
console
.
log
(
error
);
}
else
{
console
.
log
(
travels
);
}
});
app
.
use
(
logger
(
'dev'
));
app
.
use
(
express
.
json
());
app
.
use
(
express
.
urlencoded
({
extended
:
false
}));
...
...
@@ -23,12 +135,12 @@ app.use('/', indexRouter);
app
.
use
(
'/users'
,
usersRouter
);
// catch 404 and forward to error handler
app
.
use
(
function
(
req
,
res
,
next
)
{
app
.
use
(
function
(
req
,
res
,
next
)
{
next
(
createError
(
404
));
});
// error handler
app
.
use
(
function
(
err
,
req
,
res
,
next
)
{
app
.
use
(
function
(
err
,
req
,
res
,
next
)
{
// set locals, only providing error in development
res
.
locals
.
message
=
err
.
message
;
res
.
locals
.
error
=
req
.
app
.
get
(
'env'
)
===
'development'
?
err
:
{};
...
...
@@ -38,4 +150,5 @@ app.use(function(err, req, res, next) {
res
.
render
(
'error'
);
});
module
.
exports
=
app
;
...
...
Please
register
or
login
to post a comment