Toggle navigation
Toggle navigation
This project
Loading...
Sign in
김재웅
/
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-23 22:45:52 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
c4ec2446bc0fe55a8c2e43e1e3465534746474ef
c4ec2446
1 parent
58870de7
added getting festival information function
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
10 deletions
app.js
app.js
View file @
c4ec244
...
...
@@ -6,20 +6,32 @@ var logger = require('morgan');
var
app
=
express
();
const
mongodb
=
require
(
'mongodb'
);
const
MongoClient
=
mongodb
.
MongoClient
;
const
url
=
'mongodb+srv://hellowhales:qogudtjr`12@cluster0.7gz7l.mongodb.net/myFirstDatabase?retryWrites=true&w=majority'
;
let
db
;
app
.
use
(
express
.
urlencoded
({
extended
:
true
}))
// 터미널창 연결방법 mongo 'mongodb+srv://hellowhales:qogudtjr`12@cluster0.7gz7l.mongodb.net/myFirstDatabase?retryWrites=true&w=majority';
// db.festivals.find({"title":"가무악극으로 만나는 토요 상설공연"}) 검색방법
MongoClient
.
connect
(
url
)
//서버 열때 mongodb와 연결시키기
.
then
(
client
=>
{
console
.
log
(
'mongo connected'
);
console
.
log
(
client
);
})
.
then
(
app
.
listen
(
3000
,
()
=>
{
console
.
log
(
'3000 port on'
);
}))
.
catch
(
err
=>
console
.
log
(
err
));
MongoClient
.
connect
(
url
,
(
error
,
client
)
=>
{
// 서버열때 url 사용 mongoDB와 연결시키기
if
(
error
)
return
console
.
log
(
error
);
db
=
client
.
db
(
'myFirstDatabase'
);
// 클러스터의 데이터베이스를 db변수에 저장
app
.
listen
(
3001
,
()
=>
{
console
.
log
(
'3001 port on'
);
});
});
app
.
get
(
'/festivalList'
,
(
req
,
res
)
=>
{
// localhost:3000/festivalList 입력하면 list.ejs에 저장한 형식대로 정보 불러와짐
//디비에 저장된 festivals 라는 collection안의 데이터(제목 또는 내용 등)를 꺼내기
db
.
collection
(
'festivals'
).
find
().
toArray
((
err
,
rslt
)
=>
{
//DB에서 데이터를 찾음 festivals라는 collection안의 데이터를 꺼내게 됨
console
.
log
(
rslt
);
res
.
render
(
'list.ejs'
,
{
posts
:
rslt
});
// 찾은 데이터를 ejs 파일에 넣음
});
});
var
indexRouter
=
require
(
'./routes/index'
);
var
usersRouter
=
require
(
'./routes/users'
);
...
...
@@ -29,7 +41,7 @@ var usersRouter = require('./routes/users');
// view engine setup
app
.
set
(
'views'
,
path
.
join
(
__dirname
,
'views'
));
app
.
set
(
'view engine'
,
'ejs'
);
app
.
set
(
'view engine'
,
'ejs'
);
// express에서 view엔진을 ejs로 설정하는과정
...
...
@@ -58,12 +70,19 @@ app.use(function (err, req, res, next) {
res
.
render
(
'error'
);
});
/*
app.get('*', function (req, res) {
//DB에서 json형태의 데이터를 유저가 접속하자마자 바로 불러와서 frontend에 뿌려주기
// 렌더링할 때 frontend로 변수 뿌려주기
res.render("asd.html");
});
*/
module
.
exports
=
app
;
...
...
Please
register
or
login
to post a comment