Seokjin

[UPDATE]login,logout

const express = require('express');
const app = express();
const mongoose=require('mongoose');
let express = require('express');
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:"",
id:"",
password:"",
sex:-1,
log:false
};
let db=mongoose.connection; // 몽고디비를 어떻게 사용해야하는지를 정확히 몰라서 우선 이렇게 해보겠습니다.
db.on('error',function(){
console.log('Connection Failed!');
});
let mongoose = require('mongoose');
db.once('open',function(){
console.log('Connected!');
let app = express();
let logid=null;
const uri = 'mongodb+srv://sjieu17:tjrwls147714@cluster0.lc6pe.mongodb.net/weather_briefing?retryWrites=true&w=majority';
let db = mongoose.connect(uri, (err) => {
if (err) {
console.log(err.message);
} else {
console.log('Succesfully Connected!');
}
});
let user=mongoose.Schema({
name:String,
id:String,
password:String,
sex:Number
var UserSchema = new mongoose.Schema({
name: String,
id: String,
password: String,
gender:Number
});
let User=mongoose.model('users',user);
app.post('/signup',(req,res)=>{
let newUser=new User(req.body);
newUser.save((err)=>{
if (err) return res.status(500).json({message:"저장실패"});
else return res.status(200).json({message:"저장 성공!",data: newUser});
})
var Users = mongoose.model('users', UserSchema);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ limit: '1gb', extended: false }));
app.post('/signup', (req, res) => {
Users.findOne({ id: req.body.id, password: req.body.password }, (err, user) => {
if (err) return res.status(500).json({ message: '에러가 발생하였습니다.' });
else if (user){
res.send('이미 존재하는 사용자입니다.');
}
else{
var new_user = new Users(req.body);
new_user.save((err) => {
if (err) return res.status(500).json({ message: '회원가입에 실패하였습니다.' });
else return res.status(200).json({ message: '회원가입이 완료되었습니다.', data: new_user });
});
}
});
});
// 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: '유저 없음!' });
Users.findOne({ id: req.body.id, password: req.body.password }, (err, user) => {
if (err) return res.status(500).json({ message: '에러가 발생하였습니다.' });
else if (user){
logid={id:user.id,password:user.password,name:user.name,gender:user.gender};
return res.status(200).json({ message: '로그인 되었습니다.', data: user });
}
else return res.status(404).json({ message: '아이디와 비밀번호를 다시 확인해주세요.' });
});
});
app.post('/logout',(req,res)=>{
logid.id=null;
logid=null;
res.send('로그아웃 되었습니다.');
});
let server = app.listen(80);
\ No newline at end of file
app.listen(4000, () => console.log('Server On 4000'));
\ No newline at end of file
......