mysql 설치문제 때문에 한동안 못햇다 ㅜ.ㅜ
express의 mysql에 대해서 알고싶다면
express 홈페이지에 들어가서 mysql 관련을 찾는게 제일 빠르긴 하다.
http://expressjs.com/en/guide/database-integration.html#mysql
Express database integration
Database integration Adding the capability to connect databases to Express apps is just a matter of loading an appropriate Node.js driver for the database in your app. This document briefly explains how to add and use some of the most popular Node.js modul
expressjs.com
var connection=mysql.createConnection({
host:'localhost',
port:3306,
user:'root',
password:'1234',
database:'study_diary'
})
connection.connect();
링크에선 안나와있지만 port도 지정해주었다.
app.post('/ajax_send_email',function(req,res){
var email=req.body.email;
var responseData={};
var query= connection.query('select name from user where email="'+email+'"',function(err,rows){
if(err) throw err;
if(rows[0]){
responseData.result="ok";
responseData.name=rows[0].name;
}else{
responseData.result="none";
responseData.name="";
}
res.json(responseData)
})
//check validation about input value => select db
/*var responseData={'resule':'ok','email':req.body.email}
res.json(responseData)*/
});
쿼리문 작성해서 responseData object 생성 후 res.json(responseData)로 보내준다.
쿼리문을 확인하여서 rows에 받아서 있으면 result ok 넣는 식이다.
xhr.addEventListener('load',function(){
var result=JSON.parse(xhr.responseText);
var resultDiv=document.querySelector(".result");
if(result.result!=="ok") resultDiv.innerHTML="your email is not found"
else resultDiv.innerHTML=result.name;
}
받아준 후 처리
'공부 기록들' 카테고리의 다른 글
2020.02.19 백엔드 공부 (7) (0) | 2020.02.19 |
---|---|
2020.02.14 & 02.17 백엔드 공부(5),(6) (0) | 2020.02.18 |
2020.02.04 백엔드 공부(3) (Ajax, express, body-parser, ejs) (0) | 2020.02.05 |
2020.02.03 백엔드 공부(2) (0) | 2020.02.03 |
20.01.20 백엔드 공부(1) (0) | 2020.01.22 |