register.js 1.78 KB
var express = require('express')
var app = express()
var router = express.Router();
var path = require('path') // 상대경로
var mysql = require('mysql')

// database setting
var connection = mysql.createConnection({
    host: 'localhost',
    port : 3306,
    user: 'root',
    password : '2016104101',
    database : 'user'
})
connection.connect();

router.post('/form', function(req, res){
    // get: req.param('ID' ,'password')
    /* 
    // 입력값 콘솔 확인용
    console.log(req.body) // { ID: 'mukho', password: '22' }
    console.log(req.body.ID) // mukho
    console.log(req.body.password) // 22
    */

    // res.send("<h4>Welcome " + req.body.ID + "!</h4>")
    res.render('register.ejs', {'ID' : req.body.ID})
});

// 유사 로그인
router.post('/login', function(req, res){
    // 서버 반응
    console.log('Input ID: '+req.body.ID)
    console.log('Input PW: '+req.body.password)

    var id = req.body.ID;
    var pw = req.body.password;
    var responseData = {};
    
    var query = connection.query('select * from user where ID="' + id + '"', function(err, rows){
        if(err) throw err;
        // 입력된 ID가 DB에 존재하는가?
        if(rows[0]){
            // 입력된 password가 DB상의 password와 동일한가?
            if(rows[0].password == pw){
                responseData.result = "ok";
                responseData.ID = rows[0].ID;
                req.session.ID = rows[0].ID;
            }
            else{
                responseData.result = "pw err";
                responseData.ID = "";
            }
        }
        else{
            responseData.result = "none";
            responseData.ID = "";
        }
        console.log(responseData)
        console.log(responseData.ID)
        res.json(responseData)
    })
});

module.exports = router;