server.js 2.08 KB
let express = require('express');
let bodyParser = require('body-parser');
let mongoose = require('mongoose');

let cors = require('cors');
let app = express();
app.use(cors());

let logid=null;
const uri = 'mongodb://sjieu17:tjrwls147714@cluster0-shard-00-00.lc6pe.mongodb.net:27017,cluster0-shard-00-01.lc6pe.mongodb.net:27017,cluster0-shard-00-02.lc6pe.mongodb.net:27017/?ssl=true&replicaSet=atlas-q8oxwv-shard-0&authSource=admin&retryWrites=true&w=majority';
// const uri = 'mongodb+srv://tahmkench:dkrldnsl7@cluster0.vzipl.mongodb.net/?retryWrites=true&w=majority';

let db = mongoose.connect(uri, (err) => {
	if (err) {
		console.log(err.message);
	} else {
		console.log('Succesfully Connected!');
	}
});

var UserSchema = new mongoose.Schema({
	name: String,
	id: String,
  password: String,
	gender:String
});

var Users = mongoose.model('users', UserSchema);

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ limit: '1gb', extended: false }));

app.post('/api/register', (req, res) => {
  
  Users.findOne({ id: req.body.id, password: req.body.password }, (err, user) => {
    if (err) return res.status(500).json({ registerSuccess: '-1'});
    else if (user){
            res.json({ registerSuccess: '0'});
        }
    else{ 
          const new_user = new Users(req.body);

          new_user.save((err) => {
              if (err) return res.status(500).json({ registerSuccess: '-1'});
              else return res.status(200).json({ registerSuccess: '1'});
          });
      }
  });

});

app.post('/api/login', (req, res) => {
 
  Users.findOne({ id: req.body.id, password: req.body.password }, (err, user) => {
		if (err) return res.status(500).json({ loginSuccess: false });
		else if (user) { 
            // logid={id:user.id,password:user.password,name:user.name,gender:user.gender};
            return res.status(200).json({ loginSuccess: true });
        }
		else return res.status(404).json({ loginSuccess: false });
	}); 
});

app.post('/api/logout',(req,res)=>{
    logid=null;
    res.send('로그아웃 되었습니다.');
});

app.listen(4000, () => console.log('Server On 4000'));