Seokjin

[UPDATE]login,logout

1 -const express = require('express'); 1 +let express = require('express');
2 -const app = express();
3 -const mongoose=require('mongoose');
4 let bodyParser = require('body-parser'); 2 let bodyParser = require('body-parser');
5 -mongoose.connect('mongodb+srv://sjieu17:tjrwls147714@cluster0.lc6pe.mongodb.net/weather_briefing?retryWrites=true&w=majority'); 3 +let mongoose = require('mongoose');
6 -let session = require('express-session');
7 -app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}));
8 -app.use(bodyParser.urlencoded({ extended: false }));
9 -app.use(bodyParser.json());
10 -let logid={
11 - name:"",
12 - id:"",
13 - password:"",
14 - sex:-1,
15 - log:false
16 -};
17 -
18 -let db=mongoose.connection; // 몽고디비를 어떻게 사용해야하는지를 정확히 몰라서 우선 이렇게 해보겠습니다.
19 -db.on('error',function(){
20 - console.log('Connection Failed!');
21 -});
22 4
23 -db.once('open',function(){ 5 +let app = express();
24 - console.log('Connected!'); 6 +let logid=null;
7 +const uri = 'mongodb+srv://sjieu17:tjrwls147714@cluster0.lc6pe.mongodb.net/weather_briefing?retryWrites=true&w=majority';
8 +let db = mongoose.connect(uri, (err) => {
9 + if (err) {
10 + console.log(err.message);
11 + } else {
12 + console.log('Succesfully Connected!');
13 + }
25 }); 14 });
26 15
27 -let user=mongoose.Schema({ 16 +var UserSchema = new mongoose.Schema({
28 - name:String, 17 + name: String,
29 - id:String, 18 + id: String,
30 - password:String, 19 + password: String,
31 - sex:Number 20 + gender:Number
32 }); 21 });
33 -let User=mongoose.model('users',user); 22 +
34 -app.post('/signup',(req,res)=>{ 23 +var Users = mongoose.model('users', UserSchema);
35 - let newUser=new User(req.body); 24 +
36 - newUser.save((err)=>{ 25 +app.use(bodyParser.json());
37 - if (err) return res.status(500).json({message:"저장실패"}); 26 +app.use(bodyParser.urlencoded({ limit: '1gb', extended: false }));
38 - else return res.status(200).json({message:"저장 성공!",data: newUser}); 27 +
39 - 28 +app.post('/signup', (req, res) => {
40 - }) 29 + Users.findOne({ id: req.body.id, password: req.body.password }, (err, user) => {
30 + if (err) return res.status(500).json({ message: '에러가 발생하였습니다.' });
31 + else if (user){
32 + res.send('이미 존재하는 사용자입니다.');
33 + }
34 + else{
35 + var new_user = new Users(req.body);
36 +
37 + new_user.save((err) => {
38 + if (err) return res.status(500).json({ message: '회원가입에 실패하였습니다.' });
39 + else return res.status(200).json({ message: '회원가입이 완료되었습니다.', data: new_user });
40 + });
41 + }
42 + });
43 +
41 }); 44 });
42 -// app.post('/login',(req,res)=>{ 45 +
43 -// User.findOne({id: req.body.id, password:req.body.password},(err,user)=>{
44 -// if(err){
45 -// res.send('아이디와 비밀번호를 다시 확인해주십시오.');
46 -// }else{
47 -// res.send('로그인 되었습니다.');
48 -// };
49 -// });
50 -// });
51 app.post('/signin', (req, res) => { 46 app.post('/signin', (req, res) => {
52 - User.findOne({ id: req.body.id, password: req.body.password }, (err, user) => { 47 + Users.findOne({ id: req.body.id, password: req.body.password }, (err, user) => {
53 - if (err) return res.status(500).json({ message: '에러!' }); 48 + if (err) return res.status(500).json({ message: '에러가 발생하였습니다.' });
54 - else if (user) return res.status(200).json({ message: '유저 찾음!', data: user });// 저장되지 않은 유저도 찾았다고 나옴 49 + else if (user){
55 - else return res.status(404).json({ message: '유저 없음!' }); 50 + logid={id:user.id,password:user.password,name:user.name,gender:user.gender};
51 + return res.status(200).json({ message: '로그인 되었습니다.', data: user });
52 + }
53 + else return res.status(404).json({ message: '아이디와 비밀번호를 다시 확인해주세요.' });
56 }); 54 });
57 }); 55 });
58 app.post('/logout',(req,res)=>{ 56 app.post('/logout',(req,res)=>{
59 - logid.id=null; 57 + logid=null;
60 res.send('로그아웃 되었습니다.'); 58 res.send('로그아웃 되었습니다.');
61 }); 59 });
62 -let server = app.listen(80);
...\ No newline at end of file ...\ No newline at end of file
60 +app.listen(4000, () => console.log('Server On 4000'));
...\ No newline at end of file ...\ No newline at end of file
......