register.js
1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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;