Toggle navigation
Toggle navigation
This project
Loading...
Sign in
김건희
/
OSSW_Weather_Briefing
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
Seokjin
2022-05-16 17:01:29 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
11e838e7d2732dadf8085303e1f3953dddc23b68
11e838e7
1 parent
bcee7e7c
[update]mongoDBaccess
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
24 deletions
weather_briefing/server/server.js
weather_briefing/server/server.js
View file @
11e838e
const
express
=
require
(
'express'
);
const
mongoose
=
require
(
'mongoose'
);
mongoose
.
connect
(
'mongo://localhost:27017/testDB'
);
const
app
=
express
();
const
mongoose
=
require
(
'mongoose'
);
let
bodyParser
=
require
(
'body-parser'
);
mongoose
.
connect
(
'mongodb+srv://sjieu17:tjrwls147714@cluster0.lc6pe.mongodb.net/weather_briefing?retryWrites=true&w=majority'
);
let
session
=
require
(
'express-session'
);
app
.
use
(
session
({
secret
:
'keyboard cat'
,
cookie
:
{
maxAge
:
60000
}}));
app
.
use
(
bodyParser
.
urlencoded
({
extended
:
false
}));
app
.
use
(
bodyParser
.
json
());
let
logid
=
{
name
:
null
,
id
:
null
,
password
:
null
,
sex
:
null
,
name
:
""
,
id
:
""
,
password
:
""
,
sex
:
-
1
,
log
:
false
};
let
db
=
mongoose
.
connection
;
// 몽고디비를 어떻게 사용해야하는지를 정확히 몰라서 우선 이렇게 해보겠습니다.
db
.
on
(
'error'
,
function
(){
console
.
log
(
'Connection Failed!'
);
...
...
@@ -24,28 +30,33 @@ let user=mongoose.Schema({
password
:
String
,
sex
:
Number
});
let
User
=
mongoose
.
model
(
'users'
,
UserSchema
);
let
User
=
mongoose
.
model
(
'users'
,
user
);
app
.
post
(
'/signup'
,(
req
,
res
)
=>
{
let
newUser
=
new
User
(
req
.
body
);
newUser
.
save
(
function
(
error
,
data
){
if
(
error
){
res
.
send
(
'error'
);
}
else
{
res
.
send
(
'saved!'
);
}
});
//logid에다가 몽고디비에서 가져온 데이터를 넣고 로그인 된 상태로 만들고 싶음
newUser
.
save
((
err
)
=>
{
if
(
err
)
return
res
.
status
(
500
).
json
({
message
:
"저장실패"
});
else
return
res
.
status
(
200
).
json
({
message
:
"저장 성공!"
,
data
:
newUser
});
})
});
app
.
post
(
'/login'
,(
req
,
res
)
=>
{
User
.
findOne
({
id
:
req
.
body
.
id
,
password
:
req
.
body
.
password
},(
err
,
user
)
=>
{
if
(
err
){
res
.
send
(
'아이디와 비밀번호를 다시 확인해주십시오.'
);
}
else
{
res
.
send
(
'로그인 되었습니다.'
);
}
});
// app.post('/login',(req,res)=>{
// User.findOne({id: req.body.id, password:req.body.password},(err,user)=>{
// if(err){
// res.send('아이디와 비밀번호를 다시 확인해주십시오.');
// }else{
// res.send('로그인 되었습니다.');
// };
// });
// });
app
.
post
(
'/signin'
,
(
req
,
res
)
=>
{
User
.
findOne
({
id
:
req
.
body
.
id
,
password
:
req
.
body
.
password
},
(
err
,
user
)
=>
{
if
(
err
)
return
res
.
status
(
500
).
json
({
message
:
'에러!'
});
else
if
(
user
)
return
res
.
status
(
200
).
json
({
message
:
'유저 찾음!'
,
data
:
user
});
// 저장되지 않은 유저도 찾았다고 나옴
else
return
res
.
status
(
404
).
json
({
message
:
'유저 없음!'
});
});
});
app
.
post
(
'/logout'
,(
req
,
res
)
=>
{
logid
.
id
=
null
;
res
.
send
(
'로그아웃 되었습니다.'
);
});
\ No newline at end of file
});
let
server
=
app
.
listen
(
80
);
\ No newline at end of file
...
...
Please
register
or
login
to post a comment